Exclude Categories from Related Posts for WP

I set up the Related Posts for WP plugin at Sell with WP recently, and I’m already way happier with this than I was with the previous plugin I’d been using. Pricing starts at $39, and is well worth it if you want to feature related content across your site. You can even display related content from other post types.

For example, if we had a “portfolio” display at SkyVerge to display client projects that we’d worked on, we could include our own plugins that had been used, and they could display both other related projects as well as products in our WooCommerce shop (our plugin sales pages). The ability to link other post types as related is a pretty unique feature that I’m looking to take more advantage of in the future.

I only had one problem while using the plugin, which I’ll show you how to address here. I didn’t like that I couldn’t exclude some post categories from displaying related posts. At SellwithWP.com, I write up a weekly roundup of all of the eCommerce news that’s related to WordPress for the week. Since these posts will almost always have other weekly roundups as related posts, it’s a bit silly to display 4 more roundups right below the post content (I already have a category for these that the reader can view).

I used the code snippet below to omit my Weekly Roundup category from the related posts display, and you can use the same thing to exclude one or more categories when displaying related posts. If you’re not sure how to add code to your site, here’s a quick tutorial on doing so.

Remove Related Posts for WP for posts in a specific category

You’ll want to use this snippet, but replace the slug in the is_category() check with the slug for the category you’d like to exclude from displaying related posts (they’ll be displayed on all others).

Don’t add the last few lines, as they’re only there as a guide. If you’d like to exclude more than one category, you’ll replace line 6 with the updated line. The is_category() check will accept an array of category slugs if you need to check for more than one. If you’re excluding multiple categories, use this instead and then enter a comma-separated list of all of the category slugs you’d like to exclude.

Simple! The rest of the setup was easy, and I highly recommend Related Posts for WP for displaying related content across your site.