To do that, you’ll need a custom query – WordPress doesn’t have a ready-made function for getting that type of data from the termmeta
table.
The custom query uses get_col()
which allows you to get an array of the term_ids
where the meta_key
and the meta_value
match the values you have in the question. Notice that the query uses prepare()
which is good practice for security.
More info on $wpdb
can be found on its Codex page.
Providing you get some results back, you can then loop through them, and using WordPress’ get_term
and get_term_link
functions, output the list of links you’re looking for.
global $wpdb;
// custom query to get all the term_ids from the termmeta table
$query_str="SELECT term_id FROM " . $wpdb -> prefix . 'termmeta WHERE meta_key = %s AND meta_value = %s';
$meta_key = 'food_type';
$meta_value="Cat food";
$term_ids = $wpdb -> get_col( $wpdb -> prepare( $query_str, $meta_key, $meta_value ) );
// if you got results back, loop through them to create a list of links
if( $term_ids ):
$taxonomy = 'your_taxonomy';
echo '<ul>';
foreach( $term_ids as $id ):
// get the term object with $id for displaying the name
$term = get_term( $id, $taxonomy );
// get the term permalink
$url = get_term_link( $term );
echo '<li>';
echo '<a href="' . $url . '">' . $term -> name . '</a>';
echo '</li>';
endforeach;
echo '</ul>';
endif;