using_permalinks() && $q->is_main_query() && $q->is_category) { foreach($q->query as $query) { $slugs = explode('/', $query); if (count($slugs) > 1) { $category_path = rawurlencode( urldecode( $query ) ); $category_path = str_replace( '%2F', '/', $category_path ); $category_path = str_replace( '%20', ' ', $category_path ); $slugs = '/' . trim( $category_path, '/' ); $slugs = explode( '/', $slugs ); $taxonomy = 'category'; $term; foreach($slugs as $term_slug) { if (isset($term)) { $terms = get_terms($taxonomy, array('parent' => $term->term_id, 'slug' => $term_slug)); $term = null; if (count($terms)==1) $term = $terms[0]; else break; } else { $term = get_term_by('slug', $term_slug, $taxonomy); } } if (isset($term)) { $tax_query = array( array( 'taxonomy' => $taxonomy, 'terms' => $term->term_id, 'field' => 'term_id', ) ); $tax_query = new WP_Tax_Query($tax_query); $q->tax_query = $tax_query; } } } } } if ( function_exists( 'add_action' ) ) { add_action('parse_tax_query', 'fix_category_slug_hierarchy'); } ?>