Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created February 12, 2019 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/2053b932f8b4e6825e87f4e8ab5fa042 to your computer and use it in GitHub Desktop.
Save billerickson/2053b932f8b4e6825e87f4e8ab5fa042 to your computer and use it in GitHub Desktop.
<?php
/**
* Category archive query
*
*/
function be_category_archive_query( $query ) {
if( $query->is_main_query() && ! is_admin() && $query->is_tax( 'product_category' ) ) {
$ancestors = get_ancestors( get_queried_object_id(), 'product_category', 'taxonomy' );
$ancestors[] = get_queried_object_id();
$tax_query = array( array( 'taxonomy' => 'product_category', 'field' => 'term_id', 'terms' => $ancestors ) );
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'be_category_archive_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment