How to programmatically fetch posts matching a search query in WordPress?

In my plugin code I would like to perform a WP_Query (or similar) which returns all posts matching a given query string, as if the user typed that same string into the WordPress search form. Perhaps I’m just being dense, but I cannot seem to find a way to do so. I would expect to have a special parameter for WP_Query, such as matching, but I see no evidence of one.

I’ll start going through the WordPress codebase to see how it’s done internally, and I’ll post the answer here if I find it. I just thought someone might happen to know offhand.

Related posts

Leave a Reply

5 comments

  1. Passing a query variable of “s” to WP_Query with a search term will filter post results by search term:

    $query_args = array( 's' => 'disquiet' );
    $query = new WP_Query( $query_args );
    

    The corresponding SQL WHERE clause generated by this query looks like this:

    AND (((wp_posts.post_title LIKE '%disquiet%') OR (wp_posts.post_content LIKE '%disquiet%')))
    

    Default search includes the wildcards as shown above, which is most likely what you’re looking for. If you want an exact search, you can also pass a query var of "exact" => true.

    For the details see the get_posts method of WP_Query in wp-includes/query.php.

  2. I use this in my plugin:

    $query = new WP_Query(array(
        'post_type' => 'any',
        'suppress_filters' => TRUE,
        'posts_per_page' => '-1'
    ));
    
    foreach ($query->posts as $post) {
        // ...
    }
    

    post_type is needed if you’re going to work with custom post types. suppress_filters will prevent the content from being formatted if you need to analyse it. posts_per_page will return all posts, not the default per page.

  3. Something like this?

    // Check the query variable is available
    if(!$wp_query) global $wp_query; // If not, global it so it can be read from
    
    // Your custom args
    $args = array( 'the_title' => $search_term );
    
    // Merge the custom args with any for the query already
    $args = array_merge( $args , $wp_query->query );
    
    // Now do the query
    query_posts( $args );
    

    Or you could try this:

    $query = array (
        'the_title' => $search_term
    );
    
    $queryObject = new WP_Query($query);
    // The Loop...
    
  4. I believe you are looking is this compare

    $args = array(
        'post_type' => 'product',
        'meta_query' => array(
            array(
                'key' => 'color',
                'value' => 'blue',
                'compare' => 'LIKE'
            )
        )
     );
    

    from wordpress documentation

    compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default value is '='.
    
  5. This is a simpler and easier way to do search:

    $query = "
            SELECT      *
            FROM        $wpdb->posts
            WHERE       $wpdb->posts.post_title LIKE '$param2%'
            AND         $wpdb->posts.post_type = 'wp_exposants'
            ORDER BY    $wpdb->posts.post_title ";
     $wpdb->get_results($query);