Posted on 1 December, 2008 By Kim Woodbridge 21 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 4/26/09
  8. WordPress Plugin: Insights - For Images, Video and More
  9. (Anti) Social-Lists 2/15/09
  10. Adding WordPress 2.7 Threaded Comments to your Theme
  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]

    21 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> .


    Additional comments powered by BackType

Recent Comments

    • Davina: Hi Kim. Although I don't know all of these bloggers, I do know some and they are definitely great supporters. So this of...
    • Fábio N Sarmento: Do you have any idea how can i exclude a certain category, when the TT do the "new blog post" update, i want it show al...
    • Kikolani: Those are some good, patriotic themes that could be modified to work year round. Thanks for the round up! ~ Kristi...
    • Jannie Funster: Kim, that is just the sweetest shout-out ever. I guess no good deed with you goes unappreciaated, and in such a public ...
    • Kim Woodbridge: Hi - Is this for a fan page or a profile page? On a fan page go to Edit Page. In the list of Applications click on the ...
    • Kim Woodbridge: Hi Valerie - Sometimes when I'm really tired I just sit here and mindlessly click around the internet and the same thing...
    • Kim Woodbridge: Hi Patricia, Thank you so much for the kind words - I really appreciate it and you comment has made my day :-) Hav...
    • Kim Woodbridge: Hi John - Jeff is wonderful and a really nice person. They already have some great stuff on the new site and I can't wa...