Site icon Hip-Hop Website Design and Development

is_main_query() sudden outcomes

I would like so as to add product filtering utilizing WooCommerce Attributes through URL vars, however have a small situation.

Whereas the under perform will work, including is_main_query() as a situation is stopping this to run, any concepts why this is likely to be?

This appears to be associated simply to the is_main_query() facet from testing, however from what I’ve learn, that is required to forestall the perform being utilized on different parts (for instance, the principle navigation will not load if left off).

add_action( 'pre_get_posts', '_query_by_attribute' );

perform _query_by_attribute( $question ) {

  if ( is_product_category() && $query->is_main_query() && !is_admin() && false !== strpos($_SERVER['REQUEST_URI'], '?') ) {

    // Setup tax array
    $tax_query = array();

    // Get present taxonomy object and add to tax array
    $object = get_queried_object();

    $tax_query[] = array(
      'taxonomy' => $object->taxonomy,
      'subject'    => 'slug',
      'phrases'    => $object->slug
    );

    // Parse URL for varibles
    $elements = parse_url($_SERVER['REQUEST_URI']);
    parse_str($elements['query'], $vars);

    foreach($vars as $key => $worth) {

      $tax_query[] = array(
        'taxonomy' => $key,
        'subject'    => 'slug',
        'phrases'    => $worth
      );

    };

    // Output question
    $query->set(
      'tax_query', $tax_query
    );

  }

}