带有 and 或 条件的 WordPress 分类查询

标签 wordpress wordpress-theming taxonomy-terms

我有产品帖子类型,并且我有product_cat是分类法。在product_cat中,我有红色、硬质、软质、钢笔整体。

所以我必须让产品变成红色,并且它是硬的还是软的

我怎样才能得到这个?

对于同时属于红色和硬软的产品,我可以使用以下查询

 $args = array(
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tax_query' => array(
          'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                     'field' => 'slug',
                     'terms' => 'red'
                      ),
                     array(
                     'taxonomy' => 'product_cat',
                     'field' => 'slug',
                     'terms' => 'hard'
                      ),
                     array(
                     'taxonomy' => 'product_cat',
                     'field' => 'slug',
                     'terms' => 'soft'
                      )
                      ),
                     'post_type' => 'product',
                     'orderby' => 'title',
                     );

但是我需要的是红色是必须的,并且无论是软的还是硬的。 即 (red && (soft||hard ))

请帮忙。

最佳答案

你可以尝试这样:

'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'red' ),
        ),
        array(
            'relation' => 'OR',
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => array( 'hard' ),
                ),
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => array( 'soft' ),
                ),
        ),
    ),

未经测试,但应该可以工作!

如果没有,这里有一些有用的链接:

https://codex.wordpress.org/Class_Reference/WP_Query

https://10up.com/blog/2013/wordpress-mixed-relationship-taxonomy-queries/

关于带有 and 或 条件的 WordPress 分类查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50002948/

相关文章:

Windows Azure 上的 PHP 网站不显示 IE 11 模式

javascript - Gravity Forms & ReactJS - 将表单加载到模态并提交

php - 显示 WooCommerce 自定义产品属性和单个产品的所有条款

php - WooCommerce 根据产品类别替换购物车/结帐中的 "Available on backorder"

wordpress 3.5 自定义帖子类型,自定义元框,有条件地加载脚本

php - 从 WooCommerce 3+ 的轮播中排除 'hidden' 查询产品

javascript - jQuery 修复 "Uncaught TypeError: $ is not a function"错误

javascript - 菜单图标位置不同

WordPress 插件的 CSS 显示在某些浏览器上,而不是其他浏览器

wordpress - 如何将 LTR wordpress 主题更改为 RTL