WordPress: Get IDs of *ALL* post tags

WordPress contains a function for getting the tags associated with a post (get_the_tags) but none for retrieving the IDs of all site wide available tags.

The solution to this problem is quite easy if you remember that post tags are nothing else but pre-defined taxonomies and that you can use the function get_terms to retrieve the terms of a taxonomy:

$tag_ids = get_terms( 'post_tag', array('fields' => 'ids', 'get' => 'all') );

Funny enough there is a function for getting all category IDs (get_all_category_ids).


Comments

3 responses to “WordPress: Get IDs of *ALL* post tags”

  1. And that’s exactly what get_all_category_ids() does internally, except it passes ‘category’ instead of ‘post_tag’ for the taxonomy name.

    Seems like there should be a get_all_term_ids( $tax_name = 'category' ) function added to core, and possibly deprecate get_all_category_ids.

    1. The problem seems to be that most people do not know how WordPress stores the tags in the database. They look for a table like wp_tags and functions like get_all_tag_ids(). They do not connect “tag” and “taxonomy” and therefore a function called get_all_term_ids would not help them.

      Nevertheless such a function would make the logic of the WordPress functions more consistent. Sounds like a good idea.

  2. And how would this code look like to show ALL Tag-IDs of the actual post?

Leave a Reply

Your email address will not be published. Required fields are marked *