I’m really impressed with Automattic’s Jetpack, and have recently used it for one of my clients. We’re using the Related Posts module to show 3 related posts at the bottom of each single post, and while that’s fine for many of their categories, this client wanted a featured category to only show other posts from this featured category as the related items.
While there is a way to exclude a category from showing up in Related Posts, there has been no documented way to only use one specific category for Related Posts. However, it is possible by excluding all categories except for the featured category. Here’s the code!
For this code, my featured category has an ID of 721. This code makes it so posts in category 721 will only get related posts from category 721.
function jetpackme_filter_exclude_category( $filters ) { if(in_category('721')) { $args = 'exclude=721'; $categories = get_categories( $args ); foreach($categories as $category) { $filters[] = array( 'not' => array( 'term' => array( 'category.slug' => $category->slug ) ) ); } } return $filters; } add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_category' );
You can repeat lines 2-8 for each category that you need to isolate.