I’ve searched all over the place and scoured the API. All I’m coming up with are ways to get POSTS by a TAG, but not get the TAGS of a SPECIFIC post by its ID.
What I’m trying to do is pretty simple – I have a post and I want to check if it has a specific tag – ‘specialtag’ – and then do something on that page if it has this tag. Nothing seems to point me in the right direction. Is there something I’m not seeing?
It’s simple, you can use
get_the_tags
function like this :Example 1:
If you have the post ID you can retrieve the tag associated with the post by this code
Example 2:
retrieve the tag associated with the post inside the loop
this function is used in The Loop, then no ID need be passed
Example 3:
retrieve the tag associated with the post outside the loop
get_the_tags
will fetch all of the tags for a particular post. It can take a post ID as a parameter for use outside of the loop:https://codex.wordpress.org/Function_Reference/get_the_tags
If all you want to do is determine if a post has a particular tag, you can use
has_tag
:https://codex.wordpress.org/Function_Reference/has_tag
If you need to get tags by post id you could use the followng function. This will work on anywhere since the method is based on database querying.