Related Posts Showing Unrelated Pages
Posted by Ken Cheung on Monday, June 26, 2006 in How-to, WordPress : : 13 commentsI 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:
- Log into the admin area of your WordPress blog.
- 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:
. "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') ";
- 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:
Thanx! This was exactly was I was looking for!
This does work, but for me it messes up the plugin control menu.
Interesting. The plugin control menu remains the same for me.
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
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
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.
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/
[…] 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. […]
[…] 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. […]
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.
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.
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
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!