WordPress Plugins and Themes

Get Custom Taxonomy ID

3 Replies ·

  1. Hi,

    I created a custom field for custom taxonomies and I am trying to output the contents of that field on every post (about 60 with unique descriptions) with that custom taxonomy. I have the following code:

    $term_id = 1; // eg 1 is the ID of the custom term
    the_field('store_description', 'store_'.$term_id);
    

    Does anyone know how I can pull the term id from a particular custom taxonomy (I.E. replace the 1 above with the actual id of the custom taxonomy)?

  2. Justin Tadlock

    I think I may be getting lost in some of the terminology here. So, let me try to break this down to make sure I’ve gotten it right.

    You’ve created a term meta key named store_description for the terms of a particular taxonomy. And, you want to get a term ID when viewing a post.

    What’s unclear is if you’re looking for a specific term ID or all of the terms.

    I’m guessing you just want to get all of the terms, which you’d do via get_the_terms(): https://developer.wordpress.org/reference/functions/get_the_terms/

    Example:

    $terms = get_the_terms( get_the_ID(), 'your_taxonomy_name' );
    
    if ( $terms && ! is_wp_error( $terms ) ) {
    
        foreach ( $terms as $term ) {
    
            $meta = get_term_meta( $term->term_id, 'store_description', true );
        }
    }
    
  3. deadhippo

    Thanks. Where would I put this code in relation to the code I posted above. Wherever I tried it, it just broke my page.

  4. Justin Tadlock

    I don’t have enough of your code to say for sure. I’d need to see more. Generally speaking, I thought you’d be replacing your earlier code with new code.