RSS feed blog feed : : del.icio.us : : newsletter : : free magazines : : find a job

Related Posts Showing Unrelated Pages

Posted by Ken Cheung on Monday, June 26, 2006 in How-to, WordPress : : 13 comments

I finally got around to hacking the Related Posts plugin for WordPress. I love the plugin, but on some posts it listed pages like my contact form or email newsletter confirmation as related pages. None of my "pages" are related to any of my posts and I don't want my pages listed as related to my posts.

I looked on the Wasabi site (home of the Related Posts plugin), but didn't find anything other than a comment from someone with the same problem. The question was posted three months ago and it was never answered. So, I looked at the plugin code and came up with a quick solution.

To disable pages in your related results, just follow these five steps:

  1. Log into the admin area of your WordPress blog.
  2. Click on Plugins, then click on Plugin Editor.
  3. On the right is a list of your plugin files. Click on Related Posts.
  4. In the Editing related-posts.php text window, scroll down until you find this line:

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

    Its almost half way down (its a few line below // Primary SQL query). If you are using Windows, you can do a search by hitting ctrl-F and searching for static. It's line 92 if you are using a text editor on your computer.

    Change 'publish', 'static' to just 'publish'
    Static refers to pages and publish refers to posts. The new line should read:

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

  5. Click the Update File button and you are done.

That's it. Your pages will no longer be listed as related to any of your posts.

If you found this page interesting, bookmark and share it on:
del.icio.us : : digg : : yahoo : : furl : : reddit : : newsvine : : spurl : : blink : : simpy : : blogmarks

Posts of Interest

13 Comments »

Comment by kris
2006-08-29 05:43:08

Thanx! This was exactly was I was looking for!

 
Comment by kris
2006-08-30 00:48:23

This does work, but for me it messes up the plugin control menu.

 
Comment by Ken Cheung
2006-08-30 07:53:55

Interesting. The plugin control menu remains the same for me.

 
Comment by David Vanderlip
2006-08-31 15:59:01

Ken -

Thanks for this fix (enhancement). I would like to omit posts from a certain categories, but don't have the knowledge or skills to make it happen. Any ideas?

-David

 
Comment by Ken Cheung
2006-08-31 23:11:13

David,

I took a look and could not find an easy way to do. A hack will require using a table not currently used in the related posts plugin. I'll see what I can do over the weekend (if I have time).

Ken

 
Comment by David Vanderlip
2006-08-31 23:31:14

Ken -

Thanks a bunch. I guarantee that a workable solution will yield a PayPal Transfer to buy your morning coffee and bagel, or a six-pack of beer (maybe just domestic), or the #9 Menu at Taco Bell. Whatever works for you. :-) It is not much, but a way to show a little appreciation. If it turns out to be to much work, thanks for giving it a shot anyway.

 
Comment by Ken
2006-09-03 19:22:00

OK David. It was too long to post in a comment so I created a new post:

http://izachy.com/2006/09/03/excluding-categories-from-the-related-posts-plugin/

 
2006-09-18 23:09:48

[…] Like many of you, I use the Related Posts plugin. I've even hacked the plugin a couple of times to deal with unrelated pages showing up as related and to exclude certain categories from the results. Today, I'm going to show you how to hack the plugin to deal with "no related posts" results. […]

 
2006-10-18 15:55:10

[…] If you are a fan of the Related Posts plugin, then I think you will like the Similar Posts WordPress plugin. The Similar Posts plugin is based on the Related Posts plugin, but with extra functionality. Rob Marsh, the developer, even used a couple of my ideas (excluding categories and unrelated pages) in his plugin. […]

 
Comment by mod
2006-11-18 15:49:24

kris - "This does work, but for me it messes up the plugin control menu."

I would assume that you might have cut or pasted in more then was needed. I would copy a new copy of the plug-in over again, then try re-editing.

 
Comment by Che-Cheh
2006-11-19 00:05:03

Hi there, do you know where to paste ?
I've tried to paste it in index.php and single.php but it's not correct.

 
Comment by Ken
2006-11-19 11:39:52

Che-Cheh, if you are talking about the hack mentioned about, you make the changes in the plugin file itself. If you are talking about the code to call in the plugin, you can put it almost anywhere you want…index.php, single.php, home.php, etc

 
Comment by JoLynn Braley
2007-03-30 22:13:14

Thanks very much Ken, I've been wanting to implement the related posts plugin but until now it didn't work like I wanted it to. I used your code to eliminate category 1 (my pages — about, disclaimer, etc.) and that did the trick. I don't know why but when I initially changed ( 'publish', 'static' ) to
( 'publish' ) it didn't make a difference. It could be the new version of wordpress (2.1.2) though, since your post was last year. Either way it's settled now. :)

Thanks again, I'm very happy to have this working!

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.

iZachy Newsletter

Don't have time to visit iZachy everyday? Then sign up for my free newsletter. I'll send you an email when I have something to share with you. Your email address will be kept confidential and I will not share, sell, or rent it to anyone. You can unsubscribe at any time by clicking a link in the email.

Enter your email address to sign up for my newsletter: