PHP / MySQL – Search query

I need help making a search query for comments (it’s for a WordPress site).

the comments are retrieved this way- if user is logged in:

Read More
       $comments = $wpdb->get_results($wpdb->prepare("
       SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d
       AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0'))
       ORDER BY comment_date_gmt", $post->ID, $user_ID));

if not:

       $comments = $wpdb->get_results($wpdb->prepare("
       SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d
       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));

so I how can I filter comments that contain a specific search string, like $_GET['search_query'] ?

this is WP’s database structure. The search string I’m looking for is in comment_content

Related posts

Leave a Reply

3 comments

  1. use LIKE

     $comments = $wpdb->get_results($wpdb->prepare("
           SELECT * FROM $wpdb->comments WHERE comment_content LIKE ('%$_GET['search_query']%')
    and comment_post_ID = %d
           AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0'))
           ORDER BY comment_date_gmt", $post->ID, $user_ID));
    
  2. thanks, I’ll try. btw what does the percentage sign do before and after $_GET ?

    It’ll match any number of characters before and after the search string.