WP_Query returns no results

时间:2012-11-22 作者:littledynamo

我正在使用wp\\u query创建一个自定义查询来检索搜索结果,但它返回0个结果。代码如下:

$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();
当我通过UI上的搜索框搜索单词“the”时,它会返回37次点击,但使用自定义查询搜索单词“the”会返回0。所以,我怀疑我的查询一定有问题。

结果:共个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 ( ) ) 

5 个回复
SO网友:Tom J Nowell

让我们用另一个答案中张贴的更好的形式

$args = array(
    \'s\' => \'the\',
    \'posts_per_page\' => 5,
    \'paged\' => 1
);
$custom_query = new WP_Query($args);
我强烈建议您将WP\\u Query与参数数组一起使用,并像这样通过构造函数传入。

让我们仔细看看你的论点。

\'paged\' => 1
Paged是要显示的页码。

显示第2页。你在和电脑打交道,第一个数字是0, 不1.

更改为:

\'paged\' => 0
你还错过了一个wp_reset_postdata(); 你从来没有指定过帖子类型、帖子状态以及搜索框是进行标准搜索还是通过插件修改的搜索

SO网友:Toby

尝试添加&post_type=any 到您的查询

$query = \'s=the&posts_per_page=5&paged=1&post_type=any\';

SO网友:David Gard

试试这个,然后添加你的循环-

$args = array(
    \'s\' => \'the\',
    \'posts_per_page\' => 5,
    \'paged\' => 1
);
$custom_query = new WP_Query($args);
启动查询时需要传递参数,否则只需设置一个空白查询,然后使用$custom_query->query($args); 只是告诉查询要使用什么参数,实际上并没有运行查询。

SO网友:s_ha_dum

您的代码对我来说很有用,无论是您编写的代码还是几个速记版本(New WP_Query($query)New Wp_Query(\'s=the\')), 这很有道理。这些应该是等效的。插件或主题中一定有某种原因导致了这种情况。查找操纵WP\\U查询的代码--posts_where, posts_clauses, pre_get_posts, etc. 我认为这不是一个真正的WP\\U查询问题。

SO网友:MZAweb

好像有个插件或什么东西挡道了。尝试将suppress\\u filters=true添加到查询参数中,以查看这是否可以修复它。如果可行,请检查哪些插件/主题干扰了您的查询。

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post