最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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'
            )
        )
    );
}
发布评论

评论列表(0)

  1. 暂无评论