I'm working on a WooCommerce bookstore site with a custom taxonomy.php
template that generates different archive pages based on a category called "Highlights". So, for example, there is a "Best Sellers" archive page that corresponds to /books/product-category/highlights/best-sellers/
, producing a list of products assigned to the "Best Sellers" sub-category of "Highlights". On these archive pages, I'd like to include a filter than enables fine-tuning these product lists by a different category called "Topic" (normal WP category, not a custom taxonomy). To get things going, I've been trying to get a $_GET
parameter working, such that /highlights/best-sellers/?topic=art
would display Best Sellers of that category.
I initially tried using pre_get_posts
to no avail, then attempted using the filter woocommerce_product_query_tax_query
a described here. The function I've been trying, with various actions/filters is as follows, in functions.php
:
function filter_product_topic($query) {
if( is_admin() ) return;
if( !$query->is_main_query() ) return;
$tax_query = $query->get('tax_query');
if( isset($_GET['topic']) ) {
$tax_query[] = array(
'taxonomy' => 'topic',
'field' => 'slug',
'terms' => $_GET['topic'],
'include_children' => true,
'operator' => 'IN'
);
}
$query->set('tax_query', $tax_query);
}
Basically, I ultimately want to selectively invoke this additional query parameter with the filters, to further refine the archive results. So, checking a box for "Art" would refine the archive results in this way. Does anyone know how I might be able to do so? Thank you for any assistance!