<?php | |
function exclude_product_cat_children($wp_query) { | |
if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) { | |
$wp_query->set('tax_query', array( | |
array ( | |
'taxonomy' => 'product_cat', | |
'field' => 'slug', | |
'terms' => $wp_query->query_vars['product_cat'], | |
'include_children' => false | |
) | |
) ); | |
} | |
} | |
add_filter('pre_get_posts', 'exclude_product_cat_children', PHP_INT_MAX ); |
This code works fine, but it breaks admin products search in backend. :/
same problem. any help?
You can additional conditional tags to make sure the filter is applied only on frontend get_posts and not backend posts.
hi! i corrected as @ibndawood say adding a conditional tag. like this:
`<?php
function exclude_product_cat_children($wp_query) {
if ( is_product_category() ) {
if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
$wp_query->set('tax_query', array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $wp_query->query_vars['product_cat'],
'include_children' => false
)
) );
}
} }
add_filter('pre_get_posts', 'exclude_product_cat_children', PHP_INT_MAX );`
more info: https://docs.woocommerce.com/document/conditional-tags/
@durbonca this code is returning the error below. Any ideas?
`Your PHP code changes were rolled back due to an error on line 0 of file Unknown. Please fix and try saving again.
Exception thrown without a stack frame`
This code works fine, but it breaks admin products search in backend. :/