Query multiple custom taxonomy terms in Wordpress 2.8? Query multiple custom taxonomy terms in Wordpress 2.8? wordpress wordpress

Query multiple custom taxonomy terms in Wordpress 2.8?


Apparently query_posts cannot help in this specific situation. (Hopefully it will be added in future versions of Wordpress!) The solution is to use a custom select query like the following:

SELECT * FROM $wpdb->postsLEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish'AND $wpdb->term_taxonomy.taxonomy = 'technologies'AND $wpdb->terms.slug = 'php' OR $wpdb->terms.slug = 'css'ORDER BY $wpdb->posts.post_date DESC

More information can be found at the Wordpress Codex:http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query


This is a bit of a delayed reply, but it's first on Google at the moment for "wordpress related posts by multiple terms" so thought I'd contribute my findings.

Since this question was posted Wordpress has been changed to allow for this type of query. This will give you a list of posts related by any of the custom taxonomy terms assigned to an object:

$post_cats = wp_get_object_terms(get_the_ID(), 'video_category', array('fields' => 'ids'));$args=array(    "tax_query" => array(        array(            "taxonomy" => "video_category",            "field" => "id",            "terms" => $post_cats        )    ),    'post__not_in' => array(get_the_ID()),    'post_type' => 'video',    'posts_per_page' => 8,    'caller_get_posts' => 1);$related_by_cats = new WP_Query($args);

This is my first contribution to SO, I hope it's up to standards.