具有 and , or 条件的 Wordpress 分类法查询

Wordpress taxonomy query with both and , or condition

我有 product post 类型并且我有 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/