How To Create A Bidirectional Taxonomy Query May 24, 2023 by Riston Leave a Comment One of the great benefits of the recent versions of Advanced Custom Fields is the versatility by which users can effortlessly generate custom post types and taxonomies. I have created a series of inter-related custom taxonomies in an effort to organize my thoughts and to create an ontological linking system throughout the site. Sort of a custom tool for cataloguing my varied interest. Two of these custom taxonomies are grammatical_terms and morphemes. It’s important in any discourse to define one’s terms, and breaking down terms into their components (morphemes) and ruminating their origins (etymology) can be an incredibly useful tool for assisting one in properly understanding and communicating definitions. Using ACF, I am able to easily add morphemes to grammatical_terms and display them on their respective archive pages, but what if you would like to also display associated terms on the morpheme’s archive page? It would be tedious to have to add this manually on both your term and its respective morphemes. WordPress has core functions that are useful for performing these types of operations across posts and post-associated taxonomies, but what if you are making custom taxonomy to custom taxonomy associations? This requires a bit of coding: function get_associated_terms($t_type, $meta_options){ global $wpdb; $query = 'SELECT term_id FROM `wp9e_termmeta` WHERE meta_key LIKE "'.$meta_options['key'].'" AND meta_value LIKE "%%%'.$meta_options['value'].'%%%"'; $term_ids = $wpdb->get_results( $query ); $terms = array(); foreach($term_ids as $id) { $term = get_term_by('term_taxonomy_id', $id); if($term->taxonomy == $t_type){ array_push($terms, $term); } } return $terms; } We need to create a function that builds a simple SQL query, get_associated_terms. Since I’d like to reuse this function for other taxonomies, it accepts two parameters, the taxonomy type and an options array. The options array contains a key, which is the custom fields field name, and a value, which is the id of the taxonomy being queried. To understand how this is used, in my morphemes.php template I’d like to query the terms that have been associated with that morpheme. I have added morphemes to the terms, but not terms to morphemes; therefore I build the parameters for the term query like this: $term_options = array( 'key' => 'morphemes', 'value' => get_queried_object()->term_id ); $terms = get_associated_terms("grammatical_term", $term_options); This will query rows with the meta_key morphemes whose values (which are a serialized arrays) contain the id of that morpheme. We can then filter out the associated term ids that have the custom taxonomy type grammatical_term by iterating through the returned ids and retrieving the data for these terms: get_term_by('term_taxonomy_id', $id); It’s necessary to query by term_taxonomy_id if you want to get consistent data. Querying by id or term_id doesn’t return useful data (generally returns the correct keys but the with empty values). The code returns the associated grammatical_terms successfully: I hope this solution can provide utility with building up more complex inter-taxonomic queries and relationships. If you’d like to divest yourself of the hassle of coding, or would like to schedule a consultation, please check out my site https://futurelithics.com.