Posted on 1 December, 2008 By Kim Woodbridge 25 Comments

WordPress Plugin Search Reloaded – Tweaked by Stratos

TWEAK After Party (by pygment_shots)

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.

  1. Go to Plugins > Plugin Editor
  2. Make a copy of the text or have a copy of the plugin available in case things go awry
  3. 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);
  4. 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);
  5. 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


    Related Posts:
  6. WordPress – Sort Your Search Results by Relevance Rather Than By Date
  7. (Anti) Social Lists 2/7/10
  8. (Anti) Social-Lists 7/19/09
  9. (Anti) Social-Lists 7/12/09
  10. (Anti) Social-Lists 4/26/09
  11. Posted In : WordPress Plugins | WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    25 Responses to “WordPress Plugin Search Reloaded – Tweaked by Stratos”

    Trackbacks/Pingbacks
    1. stratos.me » Blog Archive » How can a WordPress plugin be unsafe
    2. - ADiario - » How can a WordPress plugin be unsafe
    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


Recent Comments

    • Juncai: Hi there, I was trying to add an image when i share to facebook. The code above for the link rel="image_src" href=”u...
    • Carla: Kim - I'm sorry for implying that anyone choosing (not because they cant pay or not eligible) to forgo insurance is irre...
    • Jamie Dunne: ~Hi Kim, Thanks for posting this - Great info ... Ive being digging through numerous apps and code trying to do th...
    • vered - blogger for hire: Thanks... and I love the photo. :) .-= vered - blogger for hire´s last blog .....
    • Kim Woodbridge: Hi Lisa Marie Mary - I think I remember that. And I never minded a mullet on Bono ;-)...
    • Kim Woodbridge: Hi Dot - We got about 2 feet - I think there was a lot more down your way. Schools were closed today, which I thought w...
    • Lisa Marie Mary: Yes, I know - I'm a mullet dork. I admit it. LOL I had my sweet little boys wear them for about their first 5/6 years -...
    • Dot: Oh, so that's what short codes are. Hope you're managing okay with all the snow. Are they good at cleaning it up in ...