Subcategories not working when using shared base with custom taxonomy


I have a custom URL structure for my WooCommerce setup.
Essentially, I have a custom taxonomy (pwb-brand) with the same base as the default (product_cat) taxonomy. To make pagination for both product categories and brands work, I used a piece of code to help WordPress distinguish brands from product categories. This works, but creates a situation where nested categories pages return a 404. To illustrate, and work fine, pagination included, but returns a 404.
Here’s the code:

// Fix pagination
function wc_categories_might_be_brands( $query ) {
  if( ! is_admin() && isset( $query->query_vars['product_cat'] ) ){
    $term = get_term_by( 'slug', $query->query_vars['product_cat'], 'product_cat' );
    if( false == $term ){
      $query->query_vars['pwb-brand'] = $query->query_vars['product_cat'];
      unset( $query->query_vars['product_cat'] );
add_action( 'parse_request', 'wc_categories_might_be_brands' );

How do I modify it to account for subcategories or skip checking subcategory queries altogether (because brands have no nested terms)?
I’m a noob with PHP. Please explain like I’m 5.
Thank you!

, somepaulo 4 years 2020-03-25T16:52:20-05:00 0 Answers 97 views 0

Leave an answer