我有产品帖子类型,并且我有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/