我正在使用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 ( ) )
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();
你从来没有指定过帖子类型、帖子状态以及搜索框是进行标准搜索还是通过插件修改的搜索