A couple of months ago I wrote about the Search Reloaded WordPress plugin, which sorts search results by relevance rather than by date.
Recently, SDK commented and said that she would like it if Search Reloaded also searched the comments.
So, 2 weeks ago when Stratos agreed to create plugins for reasonable requests, I requested that Search Reloaded be “tweaked” so it can also search comments. And Stratos did just that. You can read about it on his website.
The plugin will search posts and comments and list all of the posts that contain the search term in either the post or the comments. When I first tried it, I was confused because I thought the comments and the posts would be listed out. Stratos, however, set me straight and told me that would be way too complicated.
To test it I needed to think of a term that would be in the comments but not in an article. I then remembered that a nasty commenter called my readers sycophants and that as joke I called a regular reader the same thing in another article. And I knew that was much too large of a word for me to ever use in an actual article. So, search on sycophants and you will get two results (well, maybe three now with this article). One result will be a post and two will be the posts that have comments containing that term. Cool, huh?
The Search Reloaded plugin, however, doesn’t have a GPL or General Public License , so Stratos couldn’t redo the plugin and redistribute it but instead provided the code that would need to be edited in the plugin file.
So, here’s what you do. You’re going to have to edit the plugin file. If you aren’t comfortable with that you can ask me or Stratos for assistance.
- Go to Plugins > Plugin Editor
- Make a copy of the text or have a copy of the plugin available in case things go awry
- Look for the following block of code. If you copy it into a text editor with line numbering it starts at line 111.
$search_query = " SELECT posts.*, CASE WHEN posts.post_title REGEXP ‘$reg_one_present’ THEN 1 ELSE 0 END AS keyword_in_title, MATCH ( posts.post_title, posts.post_content ) AGAINST ( ‘" . addslashes($query_string) . "’ ) AS mysql_score FROM $wpdb->posts as posts WHERE posts.post_date_gmt <= ‘" . $now . "’" . ( ( defined(’sem_home_page_id’) && sem_home_page_id ) ? " AND posts.ID <> " . intval(sem_home_page_id) : "" ) . " AND posts.post_password = ” AND posts.post_status = ‘publish’ AND ( posts.post_title REGEXP ‘$reg_one_present’ OR posts.post_content REGEXP ‘$reg_one_present’ ) GROUP BY posts.ID ORDER BY keyword_in_title DESC, mysql_score DESC, posts.post_date DESC LIMIT " . intval($offset) . ", ". intval($posts_per_page);
- And replace with …
$search_query = " SELECT posts.*, CASE WHEN posts.post_title REGEXP ‘$reg_one_present’ THEN 1 ELSE 0 END AS keyword_in_title, MATCH ( posts.post_title, posts.post_content ) AGAINST ( ‘" . addslashes($query_string) . "’ ) AS mysql_score FROM $wpdb->posts as posts, $wpdb->comments as comments WHERE posts.post_date_gmt <= ‘" . $now . "’" . ( ( defined(’sem_home_page_id’) && sem_home_page_id ) ? " AND posts.ID <> " . intval(sem_home_page_id) : "" ) . " AND posts.post_password = ” AND posts.post_status = ‘publish’ AND posts.ID = comments.comment_post_ID AND ( posts.post_title REGEXP ‘$reg_one_present’ OR posts.post_content REGEXP ‘$reg_one_present’ OR comments.comment_content REGEXP ‘$reg_one_present’) GROUP BY posts.ID ORDER BY keyword_in_title DESC, mysql_score DESC, posts.post_date DESC LIMIT " . intval($offset) . ", ". intval($posts_per_page);
Now, I have totally stolen this code from Statos so please visit his site and check out the other WordPress plugins he has made.
Stratos hasn’t posted his services page yet but if you are looking to hire someone to develop a custom plugin for you, his contact information is on his About page.
photo credit: pygment_shots
stratosg says
thanks Kim i really appreciate your support and help all around. as always i remain for more creative and much needed plugins!
stratosg´s last blog post – WordPress Categories for hardcores or… masochists!
Kim Woodbridge says
@Stratos – You’re welcome. Your plugins are great!
Vered - MomGrind says
I’ve been trying to limit my use of plugins as much as I can. I think they often slow down the site and I also read they can pose a security risk. Would love to read a post by you that tackles this issue by the way, if you haven’t already written one.
stratosg says
this is what i have written http://www.stratos.me/2008/11/its-the-man-my-friend-not-wordpress/
i’d like to add that what you say is true. a poorly written plugin can cause create a security whole. maybe i’ll illustrate this on a future article…
stratosg´s last blog post – Member Of The Month: November
Raju says
There must be a plugin which identifies those “badly coded” plugins installed in the website ;)
Raju´s last blog post – Finer Points of Shared Web Hosting
Matt Gio | TheOvernightSite.com says
Hey Kim, you gotta join me in this challenge I’m setting up. Go to my website to see what I’m talking about! Thanks
Kim Woodbridge says
@Vered – Poorly coded plugins are definitely a security risk. There are some, however, that I can’t do without. That’s a great idea for an article :-)
@Stratos – It’s always better to find a code solution. Unfortunately, many WordPress users don’t know how to edit the template files and are very dependent on plugins.
@Raju – LOL :-)
@Matt – Great idea. I visited and commented.
Donace says
Sweet gonna give this a test run…btw the link to his site is incorrect it has ‘%20title=’ at the end of it :p…dont know how anyone didnt notice that :p
Donace´s last blog post – Automated Video Sites
Kim Woodbridge says
@Donace – Let me know how it works out for you. And thanks for letting me know about the broken link.
Natural says
i too try to keep my plugins to a minimum. i will have to read up on this as i don’t get it. doesn’t wordpress search your comments and posts? excuse my ignorance.
Natural´s last blog post – Being Human In the Age of the Electronic Mob
Sire says
I tested the plugin and I thought it was absolutely fantastic. I’m going to install it. I’m sure stratos would have said is their was a security risk involved.
Sire´s last blog post – Being The Perfect Blog Host
Kim Woodbridge says
@Natural – Search Reloaded uses a different search algorithm than the default WordPress search – sorts search results by relevance rather than by date. This tweak makes the plugin search the posts and the comments for the keyword rather than just the posts.
@Sire – The plugin is also recommended by Joost de Valk at yoast.com I trust him completely when it comes to WordPress information.
Dee Langdon - BloggerNewbie says
Kim,
I’ve never really played around with the search feature to notice whether it turned up relevant vs dated results.
I’m not really sure when I would want to search for a comment?
Dee Langdon – BloggerNewbie´s last blog post – 5 Tips To Increase Blog Traffic
Kim Woodbridge says
@Dee – I use search on certain sites – especially ones that have written a lot of articles about WordPress. In those types of articles, there can frequently be code variations and other useful information in the comments.
But I think that the only person who really searches this site is me – when I am looking for an older article ;-)
Elana says
Kim,
This fixed definitely made comments searchable – but, it screwed up the rest of the search function.
It doesn’t seem to be searching titles anymore.
http://www.elanaspantry.com/index.php?s=chestnuts
http://www.elanaspantry.com/cooked-veggies/chestnuts-with-brussel-sprouts/
Kim Woodbridge says
Elana – That is strange. The article also has chestnuts in the text so it seems like it should have found it anyway. I’ve been trying to test it here but so far haven’t come up with a word that exists in a title but not it the body of the article. Did you leave a comment for Stratos? I’m sure he will take a look at it.
http://www.stratos.me/2008/11/search-comments-with-search-reloaded/
stratosg says
I will need to take a quick look at it. Give me a few hours cause i can’t look at it right now. I’ll be back to you soon!
stratosg´s last blog post – Get rid of the Sociable plugins. Do it yourself!
Kim Woodbridge says
Thanks Stratos :-)
Elana says
Thanks all. Just wanted to let you know that I disabled the Search Reloaded plugin and tried the default WP search, the chestnuts post was indeed found.
It definitely seems to be something with the Search Reloaded tweaks…
Chinese Girl says
I was very disappointed by the default search code which makes very hard to find relevant results, this plugin can be a cure, for some sites which is frequently updated may be searching by date is also important, is there any way to give that option too for the reader?
Chinese Girl´s last blog post – Train Shenyang-Xiliu 6666
Kim Woodbridge says
Hi – Searching by date? I’m not sure. There might be a plugin
for that. Maybe people also use the archives.
The default search in WordPress is not that good.
stratosg says
I would like to inform everyone that a plugin i just found out promises what “search reloaded” charges for now. It’s called Relevanssi http://www.mikkosaari.fi/relevanssi/
Hope this helps some!
stratosg´s last blog post – No MOTM this month
Kim Woodbridge says
Hi Stratos – Thanks! Have you tried it?
stratosg says
I tried on a test blog which is as good as not trying at all since it had no real data… From seeing it i believe it’s a very good alternative to all the search plugins… I think it’s a bit resource hungry (grumpy me :P ) but still, what’s the percentage of searches against any other request… I guess it’s just fine ;)
stratosg´s last blog post – The theme change
Kim Woodbridge says
Cool. Maybe I’ll test it this weekend. I don’t think people use
search that much so the overhead shouldn’t be that bad.