admin管理员组文章数量:1316336
I have two categories that I want to exclude from my search results, so far without luck.
I have tried adding the following piece of code but it didn't work.
$search_query = query_posts(array('category__in' => array(-22, -21)));
Here is my current code.
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$searchq = new WP_Query($search_query);
while ($searchq->have_posts()) : $searchq->the_post();
I have two categories that I want to exclude from my search results, so far without luck.
I have tried adding the following piece of code but it didn't work.
$search_query = query_posts(array('category__in' => array(-22, -21)));
Here is my current code.
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$searchq = new WP_Query($search_query);
while ($searchq->have_posts()) : $searchq->the_post();
Share
Improve this question
asked Sep 22, 2014 at 14:56
SwenSwen
1,3847 gold badges21 silver badges36 bronze badges
1 Answer
Reset to default 6You can use pre_get_posts action to exclude categories from search query.
function wcs_exclude_category_search( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search ) {
$query->set( 'cat', '-22, -21' );
}
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
You should paste this code in your theme's functions.php file.
本文标签: Exclude categories from search query
版权声明:本文标题:Exclude categories from search query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741997823a2410404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论