admin管理员组文章数量:1402989
I want to display a specific category of products on the top page of the shop page.
This was achieved with the code below.
However, with this code, when searching for products from the search console, only products in the specified category will be searched.
Is it possible to write code that invalidates the code triggered by the search console search action?
add_action('pre_get_posts','shop_filter_cat');
function shop_filter_cat($query) {
if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
$query->set('tax_query', array(
array ('taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( '#','#'), //
)
)
);
}
}
I want to display a specific category of products on the top page of the shop page.
This was achieved with the code below.
However, with this code, when searching for products from the search console, only products in the specified category will be searched.
Is it possible to write code that invalidates the code triggered by the search console search action?
add_action('pre_get_posts','shop_filter_cat');
function shop_filter_cat($query) {
if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
$query->set('tax_query', array(
array ('taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( '#','#'), //
)
)
);
}
}
Share
Improve this question
asked Mar 30, 2020 at 2:15
LMDMLMDM
112 bronze badges
1 Answer
Reset to default 0This seems to be normal behavior since your filter will apply to any query made on post type archive "product". You might be able to deactivate the filter for search query by adding the is_search condition to your filter so your function would look like so:
add_action('pre_get_posts','shop_filter_cat');
function shop_filter_cat($query) {
if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query() && !is_search()) {
$query->set('tax_query', array(
array ('taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( '#','#'), //
)
)
);
}
}
Untested. hopefully this will help !
Codex ref: https://developer.wordpress/reference/functions/is_search/
本文标签: phpDisplay a specific category of products in shop page and disable code for specific actions
版权声明:本文标题:php - Display a specific category of products in shop page and disable code for specific actions 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744621536a2616055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论