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

  1. Chris Sealey
    Posted 4 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.


  2. Chris Sealey
    Posted 4 years ago #

    Decided to take a different approach with this.

  3. webleo
    Posted 4 years ago #

    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.

  4. Andrew Tegenkamp
    Posted 4 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,

Topic Closed

This topic has been closed to new replies.

About this Topic