【问题标题】:Wordpress taxonomy query with both and , or condition带有 and , or 条件的 Wordpress 分类查询
【发布时间】:2018-10-04 18:57:23
【问题描述】:

我有 product 帖子类型,我有 product_cat 是分类法。在 product_cat 我有 red, hard, soft, pen 整体。

所以我必须让产品变红,变硬或变软

我怎样才能得到这个?

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

 $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 ))

请帮忙。

【问题讨论】:

    标签: wordpress wordpress-theming taxonomy-terms


    【解决方案1】:

    你可以试试这个方法:

    '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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 2017-12-04
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多