WordPress 2.3 has built-in support for some common and useful tag and category queries. A public query variable named “tag” allows retrieving all posts associated with one or more tags. Here are some examples.
- tag=wordpress-23 fetches all posts on this blog with the “WordPress 2.3” tag.
- tag=wordpress-23,wordpress-24 fetches posts with either the “WordPress 2.3” or the “WordPress 2.4” tag.
- tag=wordpress-23+wordpress-24 fetches posts with both the “WordPress 2.3” and the “WordPress 2.4” tag.
There are also a number of private query variables that plugins can use to perform union and intersection queries for categories and tags. Plugins can insert these queries into the global query by hooking onto the ‘parse_request’ action and calling $wp->set_query_var() from their handler.
- category__in – Accepts an array of category IDs. Fetches posts in any one of the specified categories.
- category__not_in – Accepts an array of category IDs. Fetches posts that are not in any of the specified categories.
- category__and – Accepts an array of category IDs. Fetches posts that are in all of the specified categories.
- tag__in – Accepts an array of tag IDs. Fetches posts having any one of the specified tags.
- tag__not_in – Accepts an array of tag IDs. Fetches posts that do not have any of the specified tags.
- tag__and – Accepts an array of tag IDs. Fetches posts that have all of the specified tags.
- tag_slug__in – Accepts an array of tag slugs. Fetches posts having any one of the specified tags.
- tag_slug__and – Accepts an array of tag slugs. Fetches posts that have all of the specified tags.
20 thoughts on “Taxonomy Intersections and Unions”
Thanks for this post Ryan.
can you also post how we can make the home page to show only posts from certain tags.?
I would very much like to see a code sample that can be added before the call to the_loop, that helps in doing this.
Ryan, are there any plans to provide supplementary template functions (in addition to existing ones like
single_tag_title) so as to allow theme authors to access (for example) multiple tag names through the API? Tag archives would be much improved if we could display all the queried tags, not just the first one.
This is not the wordpress bugreport page but…
There seems to be a problem with using foreign characters in wordpress tags? The swedish åäö doesn’t work in the wordpress tag system. And i got a tag subdirectory with both “sang” and “sång”.How do i fix the problem?
Holy crap! We’ve been waiting for this FOREVER!
#1 new feature, IMHO
It’s nice to see that you can use the + operator on WP tag queries.
I wonder if it’s possible to do the same with cats on 2.3. (I havent installed it yet, need to fix a bunch of stuff before upgrading)
On 2.2, you can’t query trough blog/?cat=5+3
using cat=5,3 will use OR instead of AND. Hopefully, with the new taxonomy scheme you can use this same query for cats.