Excluding Categories from the Related Posts Plugin

Someone asked me how to omit posts from certain categories when using the Related Posts plugin. He even dangled a #9 from the Taco Bell menu via Paypal. It took me about 3 hours to figure it out, so that works out a few dollars per hour. But I can't complain. A few dollars is better than nothing and I can't charge someone for learning how do something I've never done before. My background is with Microsoft Access and ASP/VBScript. I have a little experience with PHP, but never had to write SQL queries for mySQL before. So in addition to getting lunch, I also learned a little bit about mySQL.

OK, this is what I have learned. To exclude certain categories from the Related Posts plugin, do the following (if you don't want to edit your plugin file directly, you can download my modified related-posts.php file, edit the category id's on your computer (see my directions below), and then upload it to your plugin directory — backup your old version first):

  • Log into the admin area of your WordPress blog.
  • Click on Manage, then click on Categories.
  • Write down the ID numbers of the categories you wish to exclude. You will need this info later.
  • Click on Plugins, then click on Plugin Editor.
  • On the right is a list of your plugin files. Click on Related Posts.
  • In the Editing related-posts.php text window, scroll down until you find this line (before you make any changes, you should make a back up of the file [select all, copy, paste it to a text file, and save it on your computer]):

$sql = "SELECT ID, post_title, post_content,"

Its almost half way down (its a line below // Primary SQL query). It’s line 85 if you are using a text editor on your computer.

Add 'DISTINCT' before 'ID' ('DISTINCT' will prevent duplicate posts from showing). You could get duplicates because you are joining another table to the existing query. The new line should read (changes in bold):

$sql = "SELECT DISTINCT ID, post_title, post_content,"

Scroll down a few lines to this line:

. "FROM $wpdb->posts WHERE "

You will need to join another table to the query so change the line to (the width of the page may result in the code wrapping into 2 lines below):

. "FROM $wpdb->posts INNER JOIN $wpdb->post2cat ON ID = post_id WHERE "

Here's the last change. Scroll down a few lines to this line:

. "AND (post_status IN ( 'publish', 'static' ) && ID != '$post->ID') ";

If you changed the line to exclude "pages" from showing as I mentioned in another post, the line will actually look like this:

. "AND (post_status IN ( 'publish' ) && ID != '$post->ID') ";

Now change the line to this (the width of the page may result in the code wrapping into 2 lines below):

. "AND (post_status IN ( 'publish' ) && ID != '$post->ID' && category_id NOT IN ( 2 )) ";

The number 2 above refers to the category id you wish to exclude. You should have this information written down from an earlier step. If the category id you want to exclude is #3, then replace the 2 with a 3. If you wish to exclude more than one category, than you will need to separate the id's with a comma. Like this (the width of the page may result in the code wrapping into 2 lines below):

. "AND (post_status IN ( 'publish' ) && ID != '$post->ID' && category_id NOT IN ( 2, 3 )) ";

This hack will exclude the categories you have listed above ( 2, 3 ). However, the hack will still show a post from an excluded category if you also placed the post in a category that is not in your excluded list. For instance, if you have a post with categories 1, 2, 3 and you only listed 2 and 3 in your excluded list, the post could still show up in the related results because of category #1.

If you are copying and pasting from this page, please check the single and double quotes when you are done pasting. Sometimes you end up copying and pasting the WordPress stylized quotes instead of the regular quotes. The stylized quotes can mess up the plugin. Replace “ and " with " and replace ‘ and ’ with the plain single quote.

When you are done, click the Update File button. Now, your excluded categories will no longer be listed as related to any of your posts.

7 thoughts on “Excluding Categories from the Related Posts Plugin

  1. Ken – Excellent! Just got in from a long flight. Will give this a try tomorrow and let you know of my progess. In the mean time check your PayPal account, and think outside the bun!

  2. David,

    I'm glad it worked. I was being to worry when I didn't hear back from you. I was hoping you wouldn't be one of those people who know enough to be dangerous.

  3. Ken, Thanks! I've used your various modifcations of Related Posts in the new version of Similar Posts. Thanks for the good ideas.

    I found that I had to use LEFT JOIN and not INNER to get the same results when no categories are excluded. But since this is the first time I've ever used a JOIN … I might be wrong.

Comments are closed.