Taxonomy Intersections and Unions

October 1, 2007
by Ryan

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.

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 Responses
  1. October 1, 2007

    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.

    Thanks
    Sadish

  2. October 1, 2007

    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.

  3. October 1, 2007

    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?

  4. October 2, 2007

    Holy crap! We’ve been waiting for this FOREVER!

    #1 new feature, IMHO

  5. October 2, 2007
    tricky permalink

    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.

Trackbacks & Pingbacks

  1. 你可能不知道的 WordPress Taxonomy 新功能 ¶ Lvx ex Cælis
  2. 你可能不知道的 WordPress Taxonomy 新功能 ¶ Lvx ex Cælis
  3. Ryan Boren on Tags and the Loop | Bloggercamp
  4. WordPress weekly digest 1st October to 7th October 2007 « westi on wordpress
  5. Famous last words of Marius » IBM Patent, RWC and RSS feed update - 13 October 2007
  6. Ad-hoc article return using WP2.3’s tagging system : Kick ass! | The Wholesale Monkey Farm V2.0
  7. links for 2007-10-19 | Ge Xiaofei
  8. WordPress 2.3.1 in detail « westi on wordpress
  9. My First Wordpress 2.3 Update Notification and Subversion Upgrade
  10. QUiXO BlOG » Nuovo aggiornamnto per Wordpress.
  11. WordPress 2.3.1 | DevStorming.com
  12. Antonio Trigiani w3bL0g - Informatica Virale » Blog Archive » Aggiornamenti Wordpress 2.3.1 security fix
  13. Metric Tensor / links for 2007-11-14
  14. Sonika’s blog » Немного о таксономии и тегах в WordPress
  15. Categories and Tags - Search and Sort « Feet up, eyes closed, head back

Comments are closed for this entry.