2

I have a Custom Post Type named 'Real estate' which has (amongst others) two different taxonomies:

  • 'Purpose' (hierarchical; terms used are 'Living', 'Working' and 'Holidays') and
  • 'Location' (non-hierarchical; e.g. 'London', 'Berlin', 'Paris').

For a search form on the 'Living' taxonomy archive I would need a list of all 'Location' terms associated with posts in the 'Living' taxnonomy term archive in order to give it back in the search form through a select. How can I, best as a list of comma separated values for an array, get all the terms for 'Location' used in the 'Living' term archive?

  • I'm doing something extremely similar to this at the moment. I don't feel I can write conclusively enough to "answer", but I'm using the WP_Query class with the tax_query parameter to query posts which belong in both taxonomies/terms/whatever you need. As this appears to be a lot of queries (potentially) I've been reading up on caching the query using transients. A similar solutions might work for you? – Dan Jul 7 '14 at 11:18
  • Hey Dan, doesn't sound as much fun as I thought - I actually believed it would be a no-brainer three-lines code solution that I just did not find. Do I really have to query all posts of tax term in order to get all values for another taxonomy? I just need it in a list like array('value1', 'value2', 'value3'); - which might just sound easier than it actually is ;). Thank you so far!! – physalis Jul 7 '14 at 12:32
  • To clarify, you're looking for the names/IDs of terms in the Location taxonomy, which are associated with posts in the Purpose->living term? If so, I believe I did this by looping through each term in the second tax (Location), trying to find one post which was present both there AND in your first tax/term. I agree, it sounds bloated - I researched a better solution but never found one. Maybe someone a little more skilled that me can help! – Dan Jul 7 '14 at 12:38
  • Yes, exactly how you describe it. I would have believed that it would be easy to accomplish with get_term_by in a way, but I got stuck in not really knowing what I was doing since I am more of a designer than a (PHP) coder. Names or IDs or slugs are fine alike, so it's more like the way of pulling the data and formatting it appropriately, so I won't be shown with places to pick that are not there houses filed under 'Living' ;). – physalis Jul 7 '14 at 14:40
  • I wrote a 120 lines function that do that in a flexible, reusable way. gist.github.com/Giuseppe-Mazzapica/11190603 – gmazzap Jul 8 '14 at 3:12
0

Ok, so I think I've got something for you! As you know, I've had the same issue, and when I was looping through each taxonomy I found that I was generating between 25-35 queries, which is crazy! So I decided to have a go at some MySQL on the term relationships table to see if there was a faster was to query the data.

With some help from a great developer I know (Mac McDonald http://wordpress.mcdspot.com/ ), I've made a function which makes one request and returns and array of term IDs in the "other" taxonomy. You can have to prepare the term IDs to query against first, like this;

//the taxonomy we want to check terms for $opposite_taxonomy_terms = get_terms( $taxonomy );

//new array, with current term id, very important!
$sibling_term_ids = array( $this_term->term_id );

foreach( $opposite_taxonomy_terms as $term )
{
    //collect all ids, ready to build the MySQL query
    $sibling_term_ids[] = $term->term_id;

}

$imploded = implode(', ', $sibling_term_ids );

$results = rd_get_opposite_terms( $imploded, $this_term->term_id );

I'm getting the terms I wish to "check" for, and then adding them to an array which must contain the main term ID you're querying against. I then implode this to get a comma separated list for the MySQL query. That's found in the rd_get_opposite_terms() function;

function rd_get_opposite_terms( $imploded = array(), $current_term_id = FALSE )
{
    if( empty( $imploded ) || ! $current_term_id )
        return FALSE;

    global $wpdb;

    //perform a query to find terms that have posts in both the $current_term_id (from one taxonomy) and terms in $imploded (term IDs from another taxonomy). Returns IDs as an array.
    return $wpdb->get_col( 'SELECT tr1.term_taxonomy_id FROM wp_term_relationships tr1 WHERE tr1.term_taxonomy_id IN ( ' . $imploded . ' ) AND tr1.object_id IN (SELECT DISTINCT object_id FROM wp_term_relationships tr2 WHERE tr2.term_taxonomy_id = ' . $current_term_id . ' ) GROUP BY tr1.term_taxonomy_id' );
}

I admit, this is near the top end of my development ability at this point in time, but I think it's a much better solution than what I originally had, and I hope it helps you too!

  • Still not having gotten my head around to try it out, but your code is still puzzling me a bit. I am still not really sure as how to use it. I need a variable that holds the array in a custom query for a WP search form, like so: – physalis Jul 16 '14 at 23:30
  • Update: Sorry for the long wait, didn't have enough time to get my head around to try it out, but your code is still puzzling me a bit. I am still not really sure as how to use it. I need a variable that holds the array in a custom query for a WP search form, like so: 'terms' => array('value1', 'value2', 'value3') I got everything working, except for the right values from the desired term :D. I declare the opposite term ( 'location' ) as taxonomy via $opposite_taxonomy_terms. However, what variable do I hold to put out the array? – physalis Jul 16 '14 at 23:49
  • Okay, I think I see through, though it doesn't throw out what I need. 1. I am sitting in my 'Purpose' term archive. 2. I define $opposite_taxonomy_terms = get_terms( 'location' ); 3. I use return $wpdb->get_col( 'SELECT tr1.term_taxonomy_id FROM wp_term_relationships tr1 WHERE tr1.term_taxonomy_id IN ( ' . $imploded . ' ) AND tr1.object_id IN (SELECT DISTINCT object_id FROM wp_term_relationships tr2 WHERE tr2.term_taxonomy_id = ' . $current_term_id . ' ) GROUP BY tr1.term_taxonomy_id' )?> to output the array, but all it shows is the word 'Array'. I am surely missing out on something? – physalis Jul 17 '14 at 0:01
  • Have you done a print_r? It's supposed to return an array of IDs you can use, but you need to see what IDs are in the array first to check if it's working. – Dan Jul 17 '14 at 11:09
  • Hey there, how exactly? print_r $wpdb->get_col('... etc.? – physalis Jul 17 '14 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.