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.