Get the term id belonging to custom taxonomy on a custom single-post-type.php template page

I have custom post-type ‘sermon’ with custom taxonomy ‘speaker’ attached to it. I wanted to assign custom meta values to the speaker taxonomy, so I am using the taxonomy meta class from Rilwis:

http://www.deluxeblogtips.com/p/taxonomy-meta-script-for-wordpress.html

Read More

I have used the class to add a metabox with the id of ‘bio’.

According to his directions, I need to use the following code in order to output the value of the ‘bio’ meta into my template:

$meta = get_option('meta_id');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;

$meta = isset($meta['term_id']) ? $meta['term_id'] : array();

$value = $meta['field_id'];

echo $value; // if you want to show

Here is what ended up working for me:

$taxmeta = get_option('speaker_meta');
if (empty($taxmeta)) $taxmeta = array();
if (!is_array($taxmeta)) $taxmeta = (array) $meta;

$taxmeta = isset($taxmeta['221']) ? $taxmeta['221'] : array();

$value = $taxmeta['bio'];

echo $value; // if you want to show

Question

In the above code you see the number ‘221’. That is the actual term-id of the taxonomy assigned to the post in question (hardcoded to test it out). What I don’t understand is how to populate the term_id dynamically.

I can’t query the url because this is simply being used on a custom post-type single page, so the term-id is not available there.

How would I modify that snippet to put in the proper term-id belonging to the ‘sermon’ that I am looking at?

thanks

edit

This function does return the proper term_id for me:

$terms = wp_get_post_terms($post->ID, "speaker");
foreach ($terms as $termid) {  
echo $termid->term_id;
} 

Using that snippet within my loop on my example page returns the value of ‘221’. If I manually put 221 in place of term_id in the function then it does pull the term meta ‘bio’ just perfectly.

Where I am still stuck

Now that I have that little snippet above which works, how do I get that to output to the term_id so the primary function will work?

$meta = isset($meta['term_id']) ? $meta['term_id'] : array();

thanks again

Related posts

Leave a Reply

3 comments

  1. you can use the get_the_terms() function to get the terms of that post in a specific taxonomy:

    $terms = get_the_terms( $post->ID , 'speaker' ); //change speaker to whatever you call your taxonomy
    //then you can use just the first term
    $term_id = $terms[0]->term_id;
    

    and now you have the term id inside $term_id.

    update

    Once you have the term_id you can use it in your function like so:

    $term_id = $termid->term_id;
    
    $meta = isset($meta[$term_id]) ? $meta[$term_id] : array();
    
  2. You can done it with wp_get_post_terms() function also:

    $terms = wp_get_post_terms($post->ID, "speaker");
    foreach ($terms as $termid) {  
      echo $termid->term_id;  
    } 
    

    it will display all terms of taxonomy for current post.

    NOTE: it must be inside the loop

  3. If you’re on the archive.php page and need the current term:

    var_dump($wp_query->queried_object);
    var_dump($wp_query->queried_object->name);
    var_dump($wp_query->queried_object->term_id);