Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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 ( ) ) 
share|improve this question
Try it with only the s=the. – s_ha_dum Nov 22 '12 at 15:16
2  
there's nothing wrong with the query alone, though the preferred method is to pass an array of arguments rather than a query string, but that wouldn't change the results. without seeing the context of where this is used it's difficult to say where the error may be. what happens if you put the query directly in a template file and print_r($custom_query);? – Milo Nov 22 '12 at 15:16
1  
Tried just s=the but no cigar. I've added the result of the print_r to the post (tried to format it as best I could!) – littledynamo Nov 22 '12 at 15:30
It is obvious why it fails: SELECT * FROM wp_posts WHERE 1=2 That is never going to return anything. Now to work out why the query looks like that... – s_ha_dum Nov 22 '12 at 15:36
1  
Apparently where 1 = 2 will retrieve the schema of a table. stackoverflow.com/questions/8149142/… – littledynamo Nov 22 '12 at 16:17
show 3 more comments

4 Answers

Try adding &post_type=any to your query

$query = 's=the&posts_per_page=5&paged=1&post_type=any';
share|improve this answer
Just tried that but same result I'm afraid. – littledynamo Nov 22 '12 at 15:33

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.

share|improve this answer
Thanks for the answer but no difference :( – littledynamo Nov 22 '12 at 15:35
Ok, so it looks like [request] is the issue. Typically a bad query is bacuase of bad query vars, but those are fine. It's possibly due to the location of the query in your code - where exactly is it located? – David Gard Nov 22 '12 at 15:44

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.

share|improve this answer

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.

share|improve this answer
Thanks for responding. I tried adding suppress_filters=true but it didn't work. It actually came through empty in the print_r i.e. [query] => Array ( [s] => the [posts_per_page] => 5 [paged] => 1 [post_type] => any [suppress_filters] => ) – littledynamo Nov 22 '12 at 16:45
Try suppress_filters=1 ? – MZAweb Nov 22 '12 at 17:41
Same result :( Seems like there's something else going on. I'm going to try query_posts to see if that makes any difference. – littledynamo Nov 22 '12 at 18:39
Don't do that! :P – MZAweb Nov 22 '12 at 19:07
Try deactivating all your plugins and test. And then activate one by one until you find the culprit. – MZAweb Nov 22 '12 at 19:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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