SearchWP Related on specific post types

SearchWP has a related posts addon for generating a list of related posts based on their relevancy engine (more information).

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.

But rather than listing a bunch of excluded post types, I often prefer listing the few post types to include.

In the code below, I have an $allowed array listing the post types I want it on. 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/code/searchwp-related-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