custom post types - Get list of terms of current taxonomy archive for another taxonomy

admin2025-01-07  10

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 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?

Share Improve this question asked Jul 7, 2014 at 10:02 physalisphysalis 3104 silver badges18 bronze badges 7
  • 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 Commented Jul 7, 2014 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 Commented Jul 7, 2014 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 Commented Jul 7, 2014 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 Commented Jul 7, 2014 at 14:40
  • I wrote a 120 lines function that do that in a flexible, reusable way. gist.github.com/Giuseppe-Mazzapica/11190603 – gmazzap Commented Jul 8, 2014 at 3:12
 |  Show 2 more comments

1 Answer 1

Reset to default 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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254087a176.html

最新回复(0)