admin管理员组文章数量:1291203
Here's the code I'm using to try and query a single taxonomy for one term, but exclude returned posts for that term that also belong to another.
In English, this is what I want: query 'resource-type' for 'testimonies', but not 'testimonies' that are also 'audio'.
Tips to tweak this code to get it to work?
<?php
$testimonials_args = array(
'post_type' => 'resource',
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'resource-type',
'field' => 'slug',
'terms' => array( 'testimonies' )
),
array(
'taxonomy' => 'resource-type',
'field' => 'slug',
'terms' => array( 'audio' ),
'operator' => 'NOT IN'
)
)
);
?>
Here's the code I'm using to try and query a single taxonomy for one term, but exclude returned posts for that term that also belong to another.
In English, this is what I want: query 'resource-type' for 'testimonies', but not 'testimonies' that are also 'audio'.
Tips to tweak this code to get it to work?
<?php
$testimonials_args = array(
'post_type' => 'resource',
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'resource-type',
'field' => 'slug',
'terms' => array( 'testimonies' )
),
array(
'taxonomy' => 'resource-type',
'field' => 'slug',
'terms' => array( 'audio' ),
'operator' => 'NOT IN'
)
)
);
?>
Share
Improve this question
asked Jun 2, 2011 at 21:06
developdalydevelopdaly
1,7972 gold badges19 silver badges33 bronze badges
3 Answers
Reset to default 1I guess its because you are trying two conditions on one taxonomy, you can allways create a custom sql query, something like this:
$querystr = "
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'resource'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'resource-type'
AND $wpdb->terms.slug = 'testimonies'
AND $wpdb->terms.slug NOT IN ('audio')
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
or you can query just by one term and exclude by has_term()
inside the loop something like this:
if (!has_term('audio','resource-type',$post->ID)){
//post without audio
}else{
//post with audio (skip or whatever)
}
no idea if this would work as i don't have enough custom tax terms locally to test this right now, but what if you change the AND relation to OR ?
edited b/c i was reading in the comments at : http://ottopress/2010/wordpress-3-1-advanced-taxonomy-queries/ that what you are asking is pretty much impossible.
however, you could maybe query all audio testimonies, spit their IDs into an array. then query all testimonies and use the array you created in the posts__not_in parameter.
You would actually put your NON IN first and it will work how you want it to.
本文标签: Query 1 taxonomy termexclude another
版权声明:本文标题:Query 1 taxonomy term, exclude another 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741512174a2382668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论