[wp-hackers] Advanced taxonomy queries

Keith Solomon ksolomon at gmail.com
Wed May 23 21:06:31 UTC 2012

I'm doing something similar for a client site.  This is the function I'm 
using in the theme:

function get_intersected_terms($tax_from, $term_from, $tax_to) {
     global $wpdb;

     $term_from = get_term_by('name', $term_from, $tax_from);

     $query = "
         SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = 
'{$tax_to}' AND term_taxonomy_id IN (
             SELECT term_taxonomy_id FROM {$wpdb->term_relationships} 
WHERE object_id IN (
                 SELECT object_id FROM {$wpdb->term_relationships} WHERE 
term_taxonomy_id = {$term_from->term_taxonomy_id}

     $term_ids = $wpdb->get_col($query);

     if (empty($term_ids))
         return array();

     return get_terms($tax_to, array('include' => $term_ids));

In the template file, I'm calling the function like this:

$showlist = get_intersected_terms('seasons', $current_season, 'shows');

$current_season is a theme option in this case.  From here, I'm using a 
foreach loop to walk through the list (which you may or may not need).

Hope this helps.

On 5/23/12 3:19 PM, Enrique Chavez wrote:
> Hi all,
> I have a custom post with 3 taxonomies ( Country, State, City ) I can get
> the terms for a single taxonomy using "get_terms( $taxonomy )".
> But let's say  that i want to get the terms in the taxonomy "state", but
> that the taxonomy 'country' is equal to 'X'
> And later search for the cities that the state is equal to Y and country
> equal to X.
> i have been reading the docs but i can't find something that help me.
> Any suggestions?
> Best Regards!
> --
> Enrique Chávez<http://tmeister.net>
> Freebies&  Updates<http://facebook.com/ecdevelopment>
> @tmeister<http://twitter.com/tmeister>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

Keith Solomon - ksolomon at gmail.com
My Blog - http://reciprocity.be/
My Flickr - http://www.flickr.com/photos/zarath0s/

More information about the wp-hackers mailing list