WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently I am working on a rating plugin. You can see the code I got so far below:

<?php if (function_exists('get_highest_rated')): ?>
<?php get_highest_rated_tag(12, 'both', 0, 10); ?>
<?php endif; ?>

But now it comes to a bit of php that I simply can't find on the web so far, so I will ask you.

At this code, you see the number 12, that is the tag id. But I can't put in the tag name.

So I was thinking of echo'ing the tagid for a certain tagname. Is this possible? This way I can get like $tagid of that certain tagname and insert it into the code above.

share|improve this question

You can use get_term_by function of wordpress.


<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?>


// Get term by name ''news'' in Categories taxonomy.
get_term_by('name', 'news', 'category')

Assign the output to some variable and use that variable instead of your tag ID. (in your case 12)


In the answer I have provided you an example. So go through the parameters first.

You can do something like this:

$tag_desc = get_term_by('name', 'news', 'post_tag');

then use, echo "<pre>"; print_r($tag_desc); echo "</pre>"; to display all the description of the tag and depending upon the parameter you set for the output field, you will get the output as object or array.

Iterate through the result and access the term_id for the tag.

Ex. $tag_desc->term_id;

share|improve this answer
I am sorry I don't understand. I don't use a certain category, I want to get it for a certain tag? The output to some variable is exactly what I want, but I don't see how I can get the tag id of $tagname by this code. Would you care to explain a bit more for me? Thanks in advance! – K Koopman Jan 17 '13 at 12:58
So are you able to get the ID by now? if you want to display the ID you can simply use echo $tag_desc->term_id; – Rohit Pande Jan 17 '13 at 13:48
Thanks for the edit, I came alittle further, but I am not that good with php. This is what I got so far:<br/> <?php if (function_exists('get_highest_rated')): ?> <ul><?php $tag_desc = get_term_by('name', $site_domain, 'post_tag'); ?> <?php get_highest_rated_tag(($tag_desc->term_id), 'both', 0, 10); ?> </ul> <?php endif; ?> yes I got the id by now, but I can't use $site_domain at this?: <?php $tag_desc = get_term_by('name', $site_domain, 'post_tag'); ?> – K Koopman Jan 17 '13 at 13:48
what is the value of your $site_domain variable? are you sure that it is a valid tag? – Rohit Pande Jan 18 '13 at 6:10
I'm using $site_domain throughout the website, it gives back the correct keywords for this theme as it's for my own network of websites. An example where it works is: <?php wp_nav_menu( array( 'menu' => $site_domain ) ); ?> – K Koopman Jan 18 '13 at 7:42

get_term_by() might help you here.

$tag = get_term_by('id', 12, 'post_tag'); //or whatever taxonomy you are using 
echo $tag->name;
share|improve this answer
Dear Rrikesh,Thank you for your reply. But I want it the other way around. I got the tag name in $tagname. And I want to get the ID of that tag. – K Koopman Jan 17 '13 at 12:55
you can use $tag = get_term_by('name', $your_term_name, 'post_tag'). Then use $tag->term_id to get the ID. – RRikesh Jan 18 '13 at 4:54
I tried this, look at the answer of Rohit Pande, but it seems it does not work, does it matter that it's in the sidebar? – K Koopman Jan 18 '13 at 8:32
Use get_query_var('post_tag')) to get your term when you are outside the loop. – RRikesh Jan 18 '13 at 9:40
Rrikesh, could you give me some more code, cause I don't know how to use this. – K Koopman Jan 18 '13 at 9:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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