Support » How-To and Troubleshooting » [Resolved] Get all tags used in custom post type

[Resolved] Get all tags used in custom post type

  • Can’t seem to find this anywhere. Does anyone know how to get a list of all tags used by a custom post type?

    So if I have posts, pages and books, I want to return a list of all tags, but only if they’re used on books. If they’re used elsewhere, don’t return them.

    Doesn’t sound too hard, but I can’t find any samples of anyone doing this.


Viewing 3 replies - 1 through 3 (of 3 total)
  • Decided to take a different approach with this.

    I faced the same problem.
    Originally, I was going to solve it with this:
    but that solution seems a bit inefficient because of the need to get each and every post’s tags and then to remove duplicates.

    I ended up doing this:

    function custom_posts_per_tag($id, $post_type){
    	$args = array(
            'post_type' => array($post_type),
            'posts_per_page' => -1,
    				'tag_id' => $id
    	$the_query = new WP_Query( $args );
    	return sizeof($the_query->posts);
    $tags = get_tags();
    global $wp_query;
    $post_type = $wp_query->get('post_type');
    foreach ($tags as $tag){
    	if (custom_posts_per_tag($tag->term_id, $post_type) > 0) {
    		/* Print tag link or whatever you wish here */

    I assume that WP_Query uses a more efficient way to filter posts by tags, than getting each post’s tag, but I might be wrong.

    I created a custom taxonomy for the custom post type and used

    $myList = get_categories('taxonomy=demographic&orderby=name');

    which gives me an object array of everything.

    Hope that helps someone,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Get all tags used in custom post type’ is closed to new replies.