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 a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Leave A Reply