0

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!

1

you need to add your topic to query_vars.

function so306156_add_query_vars_filter($vars)
{
    $vars[] = "topic";
    return $vars;
}
add_filter('query_vars', 'so306156_add_query_vars_filter');

and then, in pre_get_posts, you check for that var and act accordingly:

function so306156_manipulate_main_query($wp_query)
{
    if (function_exists('is_woocommerce') && !is_admin()) :
        if ($wp_query->is_main_query() && is_woocommerce()) {
            if (isset($wp_query->query_vars['topic'])) {
                $tax_query = $query->get('tax_query');
                $tax_query[] = [
                    'taxonomy' => 'topic',
                    'field' => 'slug',
                    'terms' => $wp_query->query_vars['topic'],
                    'include_children' => true, //true is default so you could leave that one out..
                    'operator' => 'IN' //also default
               ];
            }
        }
    }
}
add_action('pre_get_posts', 'so306156_manipulate_main_query');

im not super sure about the $wp_query->query_vars['topic'] thing in terms, try to echo that one out in the function aka echo <pre>, print_r( $wp_query->query_vars['topic'] ), '</pre>';

and now, i was thinking about another issue.. you created that custom taxonomy topic you are talking about, correct?? because you do not mention that part in your question. in your question you say, that topic is a sub-category of some sort..=?!$%/$%/(???? subcategory of product_cat?? than this stuff above won't work =)

  • thank you for the response- "topic" is not a custom taxonomy, rather a normal category in WP. I will try your approach and report back! – nickpish Jun 15 '18 at 15:04
  • well if topic is not a custom taxonomy, than all this does not make much sense. or maybe it does, please try for yourself :D but first of all, your tax_query > taxonomy must be product_cat and NOT topic... topic is a term inside product_cat taxonomy... – honk31 Jun 15 '18 at 15:11
  • Is there a different approach that would make more sense given that it's not a custom taxonomy? (I updated my question to specify that it's just a built-in category.) – nickpish Jun 15 '18 at 15:15
  • its not built-in!! woocommerce products do have their own category taxonomy called product_cat!! but you could add your custom taxonomy and also apply it to products... alomst everything is possible ;) – honk31 Jun 15 '18 at 15:16
  • Ah ok, sorry I'm not as familiar w/ WooCommerce- I see that it is a Product Category. – nickpish Jun 15 '18 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.