【发布时间】:2016-09-16 10:53:28
【问题描述】:
我正在尝试将查询扩展软件与 Wordpress 集成,但 wordpress 搜索引擎是 AND 搜索器(返回包含查询中所有单词的内容,因此查询越广泛,返回的结果越少)。 因此,当我使用带有扩展功能的查询时,搜索器返回的结果较少。 我想修改搜索的行为,并将其变成更像 OR 搜索器的东西。
示例: 对于查询“物联网,物联网”,我想要那些内容为“物联网”或“物联网”或两者兼有的帖子。
我已通读此文档 (https://codex.wordpress.org/Class_Reference/WP_Query),并尝试使用如下内容:
add_action( 'template_redirect', 'hooks_setup' , 20 );
function hooks_setup() {
if (! is_home() ) //<= you can also use any conditional tag here
return;
add_action( '__before_loop' , 'set_my_query' );
add_action( '__after_loop' , 'set_my_query', 100 );
}
function set_my_query() {
global $wp_query, $wp_the_query;
switch ( current_filter() ) {
case '__before_loop':
//replace the current query by a custom query
//Note : the initial query is stored in another global named $wp_the_query
$wp_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
//others parameters...
) );
break;
default:
//back to the initial WP query stored in $wp_the_query
$wp_query = $wp_the_query;
break;
}
}
但我不知道我需要在 wp_query 中设置哪些参数。我怎样才能做到这一点?
【问题讨论】:
-
你没有问任何问题,只是陈述了你想做什么。请参阅How to ask a good question。