WordPress Category Intersections Revisited

WordPress has included native support for intersections since (I think) version 2.3. Unfortunately, however, robust post retrieval support is only available for tags (eg. tag=A,B retrieves the union of “A” and “B”; tag=A+B retrieves the intersection of “A” and “B”).

Categories still require a hack, and the old plugins for this of course now no longer work.

To get intersection working, try adding the following line before the loop:

<?php <span style="color: #ff0000;">if ($_GET['cat']) query_posts(array('category__and'=>preg_split('/[\s,]+/',$_GET['cat'])));</span> ?>
<?php while( have_posts()) : the_post(); ?>

This applies an intersection to any list of categories separated by space, comma, or “plus” signs in the request.

See Ryan Boren’s post on WordPress intersection and union taxonomies for details on the various forms of post retrieval queries now available.

Intersecting Categories in WordPress

The default behavior of WordPress is to pull the union of multiple categories rather than the intersection. So, for example, ?cat=3,4 will show all items tagged to categories three and four. I think most people would expect this to be the reverse.

iSusi from Korea has a nice little plugin to fix this. InterCat sets the default behavior to generate, without additional joins, intersected category lists. Great for using WordPress to publish select streams of data to different sites.

Also of note: Olivier CrĂȘte’s Intersect Plugin