使用wp_Query->set设置META_QUERY和TAX_QUERY

时间:2020-02-21 作者:parvez noor

是否有一种方法可以使用wp\\u query->set()组合tax\\u查询和meta\\u查询?

我正在尝试为WooCommerce创建一个AJAX过滤器,目前我正在AJAX\\u函数中构建条件wp\\u query->set()。php文件。

我坚持使用的最后一个过滤器是在筛选price\\u范围和所选的多个标记、复选框或属性时使用的过滤器。这是我要运行的查询:

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\'
       )
    ),
}
有人能帮我怎么做吗?

提前感谢!

1 个回复
SO网友:Patryk Parcheta

只需使用另一个set() 方法:

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\'
            )
        )
    );
}

相关推荐