woocommerce offtopic - Set both meta_query and tax_query using wp_query->set

Is there a way to combine both tax_queries and meta_queries using wp_query->set() ?I am trying to create an AJAX fil

Is there a way to combine both tax_queries and meta_queries using wp_query->set() ?

I am trying to create an AJAX filter for WooCommerce, and I'm currently building out the conditional wp_query->set() in my ajax_functions.php file.

The final filter I am stuck with is when I am filtering for both a price_range, and multiple tag, checkbox or attributes selected. This is the query I want to run:

if($value['pRange'] && $value['tags']) {
  $wp_query->set('tax_query', array(
        'relation' => 'OR',
        array(
          'taxonomy' => 'product_tag',
          'field' => 'slug',
          'terms' => $value['tags'],
        ),
        array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $value['tags'],
        ),
        array(
          'taxonomy' => 'pa_branding',
          'field' => 'slug',
          'terms' => $value['tags'],
        ),  
      )  
    );
    // I want to include the meta_query in the set() method above.
    'meta_query', array(
      'relation' => 'AND',
       array(
           'key' => '_price',
           'value' => array($value["pRange"][0], $value["pRange"][1]),
           'compare' => 'BETWEEN',
           'type' => 'NUMERIC'
       )
    ),
}

Could anyone help me on how I might do this?

Thanks in advance!

Is there a way to combine both tax_queries and meta_queries using wp_query->set() ?

I am trying to create an AJAX filter for WooCommerce, and I'm currently building out the conditional wp_query->set() in my ajax_functions.php file.

The final filter I am stuck with is when I am filtering for both a price_range, and multiple tag, checkbox or attributes selected. This is the query I want to run:

if($value['pRange'] && $value['tags']) {
  $wp_query->set('tax_query', array(
        'relation' => 'OR',
        array(
          'taxonomy' => 'product_tag',
          'field' => 'slug',
          'terms' => $value['tags'],
        ),
        array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $value['tags'],
        ),
        array(
          'taxonomy' => 'pa_branding',
          'field' => 'slug',
          'terms' => $value['tags'],
        ),  
      )  
    );
    // I want to include the meta_query in the set() method above.
    'meta_query', array(
      'relation' => 'AND',
       array(
           'key' => '_price',
           'value' => array($value["pRange"][0], $value["pRange"][1]),
           'compare' => 'BETWEEN',
           'type' => 'NUMERIC'
       )
    ),
}

Could anyone help me on how I might do this?

Thanks in advance!

Share Improve this question edited Feb 21, 2020 at 20:37 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 21, 2020 at 20:25 parvez noorparvez noor 1514 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Just use another set() method:

if ($value['pRange'] && $value['tags']) {
    $wp_query->set('tax_query', array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'product_tag',
                'field' => 'slug',
                'terms' => $value['tags'],
            ),
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $value['tags'],
            ),
            array(
                'taxonomy' => 'pa_branding',
                'field' => 'slug',
                'terms' => $value['tags'],
            ),
        )
    );

    $wp_query->set('meta_query', array(
            'relation' => 'AND',
            array(
                'key' => '_price',
                'value' => array($value["pRange"][0], $value["pRange"][1]),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            )
        )
    );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744725228a4590135.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信