Getting tagid from tagname

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')): ?>
<ul>
<?php get_highest_rated_tag(12, 'both', 0, 10); ?>
</ul>
<?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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. You can use get_term_by function of wordpress.

    Syntax:

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

    Ex.

    // 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)

    EDIT

    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;