Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Class to extended WordPress' 3.0.x WP_Query to allow querying for all terms in a taxonomy.
PostsByTaxonomy class that extends WP_Query and filters to includes all posts that have any term of a taxonomy.
Author: Mike Schinkel (
This example works, just drop into the root of your website and call directly.
Use the class in your plugins or themes.
include "wp-load.php";
class PostsByTaxonomy extends WP_Query {
var $posts_by_taxonomy;
var $taxonomy;
function __construct($args=array()) {
$this->posts_by_taxonomy = true;
$this->taxonomy = $args['taxonomy'];
function posts_join($join,$query) {
if (isset($query->posts_by_taxonomy)) {
global $wpdb;
$join .=<<<SQL
INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
AND {$wpdb->term_taxonomy}.taxonomy='{$this->taxonomy}'
return $join;
$query = new PostsByTaxonomy(array(
'taxonomy' => 'category',
foreach($query->posts as $post) {
echo " {$post->post_title}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment