I have a custom field for my taxonomy called 'food_type'.

How can I display a list of my terms where this custom field's value is set to "Cat food"?

1 Answer 1


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>';

    echo '</ul>';

  • Hi shahar, thanks for your answer. Could you please edit your post to explain what your code does? See wordpress.stackexchange.com/help/how-to-answer for help on writing a good answer.
    – Tim Malone
    May 4, 2016 at 22:35
  • 1
    Hi Tim Malone. There are 3 comments already which explain what's happening, the other functions are standard Wordpress functionality which the codex does a good job of explaining. What exactly do you find unclear?
    – Steven
    May 5, 2016 at 11:05
  • BTW, I don't see anything at the link that the answer doesn't fulfil. Please explain the issue so I can correct it. Thanks.
    – Steven
    May 5, 2016 at 11:28
  • 1
    My apologies - I thought that link covered this but it appears it doesn't! When posting code it's preferable to explain how this resolves the asker's question, rather than just dumping a code block. It's the "teach a man to fish" principle - keeping in mind that many will come across this answer at a later date, possibly with a slightly different question.
    – Tim Malone
    May 5, 2016 at 20:47
  • Let us continue this discussion in chat.
    – Steven
    May 5, 2016 at 21:48

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.