PHP / MySQL - search query

I need help creating a search query for comments (this is for a WordPress site).

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

       $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 can i filter out comments containing a specific search string, for example $_GET['search_query']?

This is a WP database structure . The search string I'm looking for is incomment_content

+1
source share
3 answers

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
source

You can put them all in an array and use array_search:

http://il2.php.net/manual/en/function.array-search.php

wp google search query widge. :

http://www.lautr.com/wp-google-search-query-widget-wordpress-plugin

+1

Thanks, I'll try. By the way, what does the percent sign do before and after $ _GET?

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

+1
source

All Articles