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:


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


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?



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

3 Answers 3


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.


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();
  • For some reason the term_id is not being picked up when on the custom post-type template file. When I apply the code to the taxonomy template then it works fine. Maybe there are some rules I am missing for pulling the term_id on other template pages? --like does it have to be within the loop or something?
    – shawn
    Commented Jun 14, 2011 at 11:07
  • the code i posted assumes that $post->ID is set so yeah it should be inside the loop
    – Bainternet
    Commented Jun 14, 2011 at 14:07
  • I made absolute certain to place the code within the loop, but it does not seem to return the '221' value. I did try KOt's answer above and it does return 221, although I am not sure how to assign the output to term_id so that I can use it in my function. ideas?
    – shawn
    Commented Jun 14, 2011 at 20:58
  • @Bainternet I edited my question above to show where I am at. Can you help show me how to get term_id populated with the new snippet? I did try your snippet but it was not returning any value, even when in the loop. thanks
    – shawn
    Commented Jun 14, 2011 at 22:45
  • i updated my answer
    – Bainternet
    Commented Jun 14, 2011 at 22:54

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

  • This function is returning the proper value for me of '221' when used on this page. Now that part is working, how do I 'wrap' that output of 221 into term_id so that I can use it in my function above?
    – shawn
    Commented Jun 14, 2011 at 20:30

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.