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'].

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

  • nevermind, I figured it out :)
    – Alex
    Dec 14, 2010 at 21:46
  • Would you care to share? Dec 14, 2010 at 22:39
  • 1
    can you share the solution? so that someone else who stumble upon this question can see it?
    – ariefbayu
    Dec 14, 2010 at 22:40

2 Answers 2


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}%%')";

    $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));
    $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));
    $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" />

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.