admin管理员组文章数量:1291148
I want to use the WP_Query()
class to filter some of my posts. The problem I am facing now is handling the taxonomy query. Normally, the WP_Query()
only handle one relationship for tax_query()
(either AND or OR), but what I need is mixed use of these relationships on the tax_query()
, how can achieve it?
eg
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'taxonomy1',
'field' => 'slug',
'terms' => array( $term )
),
array(
'taxonomy' => 'taxonomy3',
'field' => 'slug',
'terms' => array( $term3 ),
'operator' => 'IN',
)
// below i want to use OR relationship
'relation' => 'OR',
array(
'taxonomy' => 'taxonomy4',
'field' => 'slug',
'terms' => array( $term4 )
),
array(
'taxonomy' => 'taxonomy2',
'field' => 'slug',
'terms' => array( $term2 ),
'operator' => 'IN',
)
)
I know the code above is not working, do I need to use WP_Query()
filter to do it? Any idea?
I want to use the WP_Query()
class to filter some of my posts. The problem I am facing now is handling the taxonomy query. Normally, the WP_Query()
only handle one relationship for tax_query()
(either AND or OR), but what I need is mixed use of these relationships on the tax_query()
, how can achieve it?
eg
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'taxonomy1',
'field' => 'slug',
'terms' => array( $term )
),
array(
'taxonomy' => 'taxonomy3',
'field' => 'slug',
'terms' => array( $term3 ),
'operator' => 'IN',
)
// below i want to use OR relationship
'relation' => 'OR',
array(
'taxonomy' => 'taxonomy4',
'field' => 'slug',
'terms' => array( $term4 )
),
array(
'taxonomy' => 'taxonomy2',
'field' => 'slug',
'terms' => array( $term2 ),
'operator' => 'IN',
)
)
I know the code above is not working, do I need to use WP_Query()
filter to do it? Any idea?
2 Answers
Reset to default 17This can be done by using the term_taxonomy_id rather than the slug, which will effectively ignore whichever taxonomy is specified and just look at the unique term_taxonomy_id field. This will allow you to effectively be able to do a mixed relationship. You'd want to use an overall relation of AND and put all the terms that should be related OR in one item using the IN operator. You will need to map the terms desired to their term_taxonomy_ids first.
$taxes = array( 'taxonomy1', 'taxonomy2', 'taxonomy3', 'taxonomy4' );
foreach ( $taxes as $tax ) {
$terms = get_terms( $tax );
foreach ( $terms as $term )
$tax_map[$tax][$term->slug] = $term->term_taxonomy_id;
}
$args['tax_query'] => array(
'relation' => 'AND',
array(
'taxonomy' => 'taxonomy1',
'field' => 'term_taxonomy_id',
'terms' => array( $tax_map['taxonomy1'][$slug] )
'operator' => 'IN',
),
array(
'taxonomy' => 'taxonomy3',
'field' => 'term_taxonomy_id',
'terms' => array( $tax_map['taxonomy3'][$slug] ),
'operator' => 'IN',
),
array(
'taxonomy' => 'taxonomy4', // gets ignored
'field' => 'term_taxonomy_id',
'terms' => array( $tax_map['taxonomy4'][$slug], $tax_map['taxonomy2'][$slug] ),
'operator' => 'IN',
),
);
Note that pre-3.5, you'd also need to specify 'include_children' => false
. See this Trac ticket for more: https://core.trac.wordpress/ticket/21228
I sugest using tax_query
as the meta_query
for multiple or/and operators like this
本文标签: wp queryMultiple relationship for multiple taxquery in WPQuery
版权声明:本文标题:wp query - Multiple relationship for multiple tax_query in WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741523324a2383319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论