Jetpack Related Posts – how to exclude multiple categories?
-
Following the instructions at https://jetpack.me/support/related-posts/customize-related-posts/ I’ve added this to my functions file:
function jetpackme_filter_exclude_category( $filters ) {
$filters[] = array( ‘not’ =>
array( ‘term’ => array( ‘category.slug’ => ‘latest-competitions’ ) )
);
return $filters;
}
add_filter( ‘jetpack_relatedposts_filter_filters’, ‘jetpackme_filter_exclude_category’ );However, I’d like to exclude at least 3 categories from ever appearing in Related posts, rather than just ‘latest competitions’. How can I do this?
Many thanks!
Di – https://superlucky.me
-
Here is how you could exclude posts from 3 categories, named “cat-1”, “cat-2”, “cat-3”:
function jetpackme_filter_exclude_categories( $filters ) { $filters[] = array( 'not' => array( 'terms' => array( 'category.slug' => array( 'cat-1', 'cat-2', 'cat-3' ) ) ) ); return $filters; } add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_categories' );
Thanks for your input Jeremy. I still can’t get this to work. I specifically want to exclude all posts in the ‘https://superlucky.me/category/competitions/’ category, which contains 4 sub categories. I’ve tried excluding the single ‘competitions’ category slug in the original code I posted, and using the 4 individual sub categories in the code you’ve given me above, but still my posts show related posts from the ‘latest competitions’ category at the bottom!
(I’ve deactivated the plug in at the moment as I don’t want people clicking on those out-of-date posts!)
sing the 4 individual sub categories in the code you’ve given me above, but still my posts show related posts from the ‘latest competitions’ category at the bottom!
Could you post the code you added to your site here so I can take a look? Could you then give me a few examples of Related Posts being displayed when they shouldn’t?
Thanks!
I have been wanting to do the exact same thing. This solution worked for me.
I was about to switch to a different plugin. Glad I decided to look on the forum first, because I prefer the way this plugin formats the results.
Thanks Jeremy. I’m using the ‘Edit functions’ plug in, this is what I have in the file:
function jetpackme_related_posts_headline( $headline ) { $headline = sprintf( '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( 'If you liked this, you might enjoy these...' ) ); return $headline; } add_filter( 'jetpack_relatedposts_filter_headline', 'jetpackme_related_posts_headline' ); function jetpackme_filter_exclude_category( $filters ) { $filters[] = array( 'not' => array( 'term' => array( 'category.slug' => 'competitions' ) ) ); return $filters; } add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_category' );
I’ve gone back to trying to exclude all posts from just one main category – competitions – which has 4 sub categories. As an example, my latest post at https://superlucky.me/2015/03/carhireoke-castles-kent/ is showing two related posts from the competitions category (one in the ‘latest comps’ sub category, one in ‘blog comp linky’ sub category). This post is also showing two related posts from ‘latest comps’ – https://superlucky.me/2015/03/budweiser-dream-goal-promotion/. I also tried replacing ‘competitions’ with ‘latest-competitions’ but that didn’t work.
Thanks for your help!
I’ve gone back to trying to exclude all posts from just one main category – competitions – which has 4 sub categories.
You will need to exclude the sub categories as well, or that won’t work.
Could you post the code you had tried to use to exclude all subcategories?
Thanks!
Thanks for your patience!
I now have this code:
function jetpackme_filter_exclude_categories( $filters ) { $filters[] = array( 'not' => array( 'terms' => array( 'category.slug' => array( 'competitions', 'instant-wins-daily-draws', 'superlucky-giveaways', 'latest-competitions', 'blog-comp-linky' ) ) ) ); return $filters; } add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_categories' );
On the same post, I still have two posts from the ‘Latest competitions’ category’ showing: https://superlucky.me/2015/03/budweiser-dream-goal-promotion/
Thanks!
Could you try to go to the Jetpack menu in your dashboard, scroll down to the bottom of the page, click on “Debug”, and then click on the Reindex button at the bottom of that page.
Let me know if it helps.
I’ve done that (it took hours to reindex!) but no difference, unfortunately. Thanks for trying to help!
it took hours to reindex!
That’s on purpose, so we don’t overload your server by trying to index all your posts at once.
Related Posts seem to be disabled at the moment. If you still experience issues when you enable the module again, could you load this URL in your browser, copy the contents of the page, and paste them here? I’ll then forward these results to one of our developers who will be able to take a closer look at the problem.
Thanks!
Here you go:
{“version”:”20141201″,”show_thumbnails”:true,”items”:[{“id”:5359,”url”:”http:\/\/superlucky.me\/2014\/10\/prizes-football-fans\/”,”url_meta”:{“origin”:6678,”position”:0},”title”:”Love football? Love comps!”,”date”:”24\/10\/2014″,”format”:false,”excerpt”:”If you love football then\u00a0you’re in luck. There ar\u2026″,”context”:”In \”Latest comps\””,”img”:{“src”:”http:\/\/i0.wp.com\/superlucky.me\/wp-content\/uploads\/2014\/10\/LoveFootballFeat.jpg?resize=350%2C200″,”width”:350,”height”:200}},{“id”:5480,”url”:”http:\/\/superlucky.me\/2014\/11\/current-pack-promotions\/”,”url_meta”:{“origin”:6678,”position”:1},”title”:”Current on-pack promotions”,”date”:”09\/11\/2014″,”format”:false,”excerpt”:”This week I decided to check out some of the curre\u2026″,”context”:”In \”Latest comps\””,”img”:{“src”:”http:\/\/i1.wp.com\/superlucky.me\/wp-content\/uploads\/2014\/11\/PromoFeat.jpg?resize=350%2C200″,”width”:350,”height”:200}},{“id”:6767,”url”:”http:\/\/superlucky.me\/2015\/03\/win-a-degustabox\/”,”url_meta”:{“origin”:6678,”position”:2},”title”:”Win a Degustabox (ends 6.4)”,”date”:”27\/03\/2015″,”format”:false,”excerpt”:”As a comper I’m a big fan of\u00a0surprises in the post\u2026″,”context”:”In \”Exclusive giveaways\””,”img”:{“src”:”http:\/\/i1.wp.com\/superlucky.me\/wp-content\/uploads\/2015\/03\/DegustaFeat.jpg?resize=350%2C200″,”width”:350,”height”:200}}]}
Unfortunately, I can’t leave the plug in active at the moment because it’s bringing up some really old posts from the ‘latest comps’ category – but if you reply here I’ll reactivate it as soon as I can. Thankyou!
Thanks!
I’ve asked one of our developers to take a look and I’ll let you know what we find!
It seems that the problem is caused by the differences between your category names and the category slugs. Could you try to use this code instead?
function jetpackme_filter_exclude_categories( $filters ) { $filters[] = array( 'not' => array( 'terms' => array( 'category.name.raw' => array( 'competitions', 'instant-wins-daily-draws', 'exclusive-giveaways', 'latest-comps', 'blog-comp-linky' ) ) ) ); return $filters; } add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_categories' );
Let me know how it goes!
Thanks for your help. Unfortunately the posts are still showing related content from ‘Latest comps’ and ‘Blog comp linky’ (sorry, I have to keep deactivating the plug in before you can see the results as the content isn’t relevant!).
One other thing – I did try a Debug in the Jetpack menu and got this response:
Jetpack Debugging Center
Testing your site’s compatibility with Jetpack…
There seems to be a problem with your site’s ability to communicate with Jetpack!
It looks like your site can not communicate properly with Jetpack.Not sure if this is affecting it?
Thanks for your patience.
Unfortunately the posts are still showing related content from ‘Latest comps’ and ‘Blog comp linky’ (sorry, I have to keep deactivating the plug in before you can see the results as the content isn’t relevant!).
Could you let me know the exact names and slugs you use for each category? You can check it under Posts > Categories in your dashboard.
- The topic ‘Jetpack Related Posts – how to exclude multiple categories?’ is closed to new replies.