SearchWP Related metabox on specific post types

SearchWP has a related posts extension for generating a list of related posts based on their relevancy engine.

The metabox appears on all post types by default, and you can use the searchwp_related_excluded_post_types filter to exclude certain post types.

This often includes post types that have no need for related posts, like ACF field groups:

Rather than listing the excluded post types, I prefer specifying the few post types to include.

In the code below, I have an $allowed array listing the post types on which I’d like the metabox to appear. It then generate a list of all other post types and excludes SearchWP Related on those.

/**
 * SearchWP Related on specific post types
 * @link https://www.billerickson.net/searchwp-related-metabox-on-specific-post-types/
 */
function ea_searchwp_related_exclude( $exclude = array() ) {
	$allowed = array( 'post', 'resource' );
	$all = array_keys( get_post_types() );
	return array_diff( $all, $allowed );
}
add_filter( 'searchwp_related_excluded_post_types', 'ea_searchwp_related_exclude' );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Comments are closed. Continue the conversation with me on Twitter: @billerickson