WP_Query returns no results

I’m using wp_query to create a custom query that retrieves search results but it is returning 0 results. Here is the code:

$query = 's=the&posts_per_page=5&paged=1';
$custom_query = new WP_Query();
$custom_query->query($query);

if( $custom_query->have_posts() ) {
        while ( $custom_query->have_posts() ) : $custom_query->the_post();
                    $this->get_article();
        endwhile;
}
else
        $this->posts_404();

When I search for the word ‘the’ through the search box on the UI, it returns 37 hits, but searching for the word ‘the’ with my custom query returns 0. So, I suspect there must be a problem with my query.

Read More

Result: of print_r($Custom_query):

WP_Query Object ( 
 [query_vars] => Array 
  ( [s] => the 
    [posts_per_page] => 5 
    [paged] => 1 
    [error] => 
    [m] => 0 
    [p] => 0 
    [post_parent] => 
    [subpost] => 
    [subpost_id] => 
    [attachment] => 
    [attachment_id] => 0 
    [name] => 
    [static] => 
    [pagename] => 
    [page_id] => 0 
    [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 
    [w] => 0 
    [category_name] => [tag] => [cat] => [tag_id] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [sentence] => [fields] => 
    [category__in] => Array ( ) 
    [category__not_in] => Array ( ) 
    [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) 
[ignore_sticky_posts] => [suppress_filters] => 
[cache_results] => 1 
[update_post_term_cache] => 1
[update_post_meta_cache] => 1 
[post_type] => any [nopaging] => 
[comments_per_page] => 50 [no_found_rows] => 
[search_terms] => Array ( [0] => the ) [order] => DESC ) 
[tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND )    
[meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => )    
[post_count] => 0 
[current_post] => -1 
[in_the_loop] => 
[comment_count] => 0 
[current_comment] => -1 
[found_posts] => 0 
[max_num_pages] => 0 
[max_num_comment_pages] => 0 
[is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => 
[is_search] => 1 
[is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => 
[query_vars_hash] => 16a0222409543c8384496148e5b60565 
[query_vars_changed] => [thumbnails_cached] => 
[query] => Array ( [s] => the [posts_per_page] => 5 [paged] => 1 ) [request] => SELECT * FROM wp_posts WHERE 1=2 [posts] => Array ( ) ) 

Related posts

Leave a Reply

5 comments

  1. Lets take the nicer form of what you have as posted in another answer

    $args = array(
        's' => 'the',
        'posts_per_page' => 5,
        'paged' => 1
    );
    $custom_query = new WP_Query($args);
    

    I strongly recommend you use WP_Query with an argument array and pass in via the constructor like this.

    Lets look at your arguments closer.

    'paged' => 1
    

    Paged is the page number to show.

    This says, show page 2. You’re dealing with computers, and the first number is 0, not 1.

    Change to:

    'paged' => 0
    

    You also missed out a call to wp_reset_postdata(); to clean up after yourself, and you never specified the post type, post status, and wether the search box is doing a standard search or a search modified by a plugin

  2. Try this, and then add your Loop –

    $args = array(
        's' => 'the',
        'posts_per_page' => 5,
        'paged' => 1
    );
    $custom_query = new WP_Query($args);
    

    You need to pass the arguments when you are initiating the Query, otherwish you are just setting up a blank query, and then using $custom_query->query($args); is only telling the query what arguments to use, it’s not actually running the query.

  3. Your code works for me, both as you have it written and in a couple of shorthand versions (New WP_Query($query) and New Wp_Query('s=the')), which makes sense. Those should be equivalent. There has to be something in a plugin or your theme that is causing this. Look for code that manipulates WP_Query– posts_where, posts_clauses, pre_get_posts, etc. I don’t think this is really a WP_Query issue.

  4. Seems like there’s a plugin or something getting in the way. Try adding suppress_filters=true to your query args to see if that fixes it. If works, check what plugin/theme is messing with your query.