Search code examples
phpwordpresstaxonomy

Query multiple custom taxonomy terms in Wordpress 2.8?


I created a custom taxonomy named 'technologies' but cannot query multiple terms like I can with categories or tags.

These querys DO work:

query_posts('tag=goldfish,airplanes');

query_posts('technologies=php');

However, neither of the following work correctly:

query_posts('technologies=php,sql');

query_posts('technologies=php&technologies=sql');

My objective: Show all posts with a technology of 'php' and all posts with a technology of 'sql'

Any ideas? Is this even possible? Thanks!


Solution

  • 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->posts
    LEFT 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