WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get all tags used in custom post type (4 posts)

  1. Chris Sealey
    Member
    Posted 3 years ago #

    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.

    Cheers.

  2. Chris Sealey
    Member
    Posted 3 years ago #

    Decided to take a different approach with this.

  3. webleo
    Member
    Posted 3 years ago #

    I faced the same problem.
    Originally, I was going to solve it with this:
    http://wordpress.org/support/topic/custom-post-types-and-tag-cloud?replies=8
    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 );
    	wp_reset_query();
    
    	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.

  4. Andrew Tegenkamp
    Member
    Posted 2 years ago #

    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,
    Andrew

Topic Closed

This topic has been closed to new replies.

About this Topic