79355006

Date: 2025-01-14 12:38:53
Score: 2.5
Natty:
Report link

Why not just skip that extra if-statement and include the !is_admin() within the first one? Like this:

function exclude_product_cat_children($wp_query) {
  if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query() && !is_admin() ) {
    $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');
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): Why not
  • Low reputation (1):
Posted by: Sebastian Bernhardtz