Comment filtering (search)

Basically I want to develop the ability to search comments on my site. A form with input field where the visitor can enter a search string, which tells WP to display only comments that contain that search phrase.

I think the best way to do this is by making a custom comments_template() function, then filtering out comments that don’t contain $_GET['search_phrase'].

Read More

Any pointers/suggestions, especially on the SQL part are welcome 🙂

Related posts

Leave a Reply

2 comments

  1. You create your own comments_template, it can be a duplicate function of the default one, the only changes are the database queries anyway. But you need your own function as WP doesn’t help you with filters here. SO, name it my_comments_template():

      $filter = mysql_real_escape_string($_POST['comment-filter']);
      if(!empty($filter)) $filter = "AND (comment_content LIKE '%%{$filter}%%' OR comment_author LIKE '%%{$filter}%%')";
    
      if($user_ID)
        $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_post_ID = %d) {$filter} AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0')) ORDER BY comment_date_gmt", $post->ID, $user_ID));
      elseif(empty($comment_author))
        $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_post_ID = %d) {$filter} AND (comment_approved = '1') ORDER BY comment_date_gmt", $post->ID));
      else
        $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_post_ID = %d) {$filter} AND (comment_approved = '1' OR (comment_author = %s AND comment_author_email = %s AND comment_approved = '0')) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author, ENT_QUOTES), $comment_author_email));
    

    then add a simple form in the comments template file:

       <form action="<?php echo get_permalink(); ?>" method="post" id="comment-filter">
         <input type="text" name="comment-filter" rel="<?php _e("Search in comments"); ?>" value="<?php echo esc_attr($_POST['comment-filter']); ?>" size="20" />
       </form>
    

    and of course replace all comments_template() function calls with my_comments_template()

    I’m implementing this code into a theme I’m working on. My code is a little bigger as I’ve added ajax & query highlighting… If you want to see it all wait for the 1.3 release of my Atom theme.

    One thing I didn’t figure out yet is how to keep $_POST[‘comment-filter’] when navigating trough comment pages. For eg. if you search for something inside 5000 comments and you get 1000 comments containing that text and broken up in pages, when you switch the page the comment filter query is lost and you get 2000 comments displayed again…

    This function is really useful on Tech related WordPress sites that have hundreds of comments on posts…

  2. I know it’s a little too long to answer here, but hope someone finds this helpful in the future.

    I had to implement a similar functionality for a project I’m working on. The best way is to make a new sql table(or a view on the wp_comments table) for storing only the comment data you want to return in the search results. Then use these undocumented actions/filters for comments: comment_{old status}to{new status} (‘deleted’, ‘approved’, ‘unapproved’, ‘spam’), approve_comment, approved_comment (run before and after approval of a comment), delete_comment, deleted_comments (run before and after permanent deletion of a comment), trash_comment, trashed_comment(runs before and after trashing a comment) to update this table(as you wouldn’t want a comment to be searched if it’s unapproved or spammed or trashed or deleted, would you ?). Then you can fire your search queries on this custom table.
    If you need examples for these actions/filters, feel free to ask me!

    Thanks,
    rutwick