Search code examples
wordpresstagscustom-post-type

How to display tags only on taxonomy page of custom post type


I have a custom post type name 'seekers' and its taxonomy named 'seekers-type'. on the 'archive-seekers.php' page I display taxonomy loop with its post counts. upon any single taxonomy i redirect user to 'taxonomy-seekers-type.php'. Here i have a section where I only want to display tags generated within seekers custom post. any help, i tried several options available on different post or resources.

$tags = get_tags();                                
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= '<li>';        
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>{$tag->name}</a>";
$html .= "</li>";
}
echo $html;

here it shows all the tags... (seekers=custom post type) and (taxonomy=seekers-types)

function seekers_cpt(){
$args = array(
    'labels' => array(
        'name' => 'Seekers',
        'singular_name' => 'Seeker',
    ),
    'menu_icon' => get_template_directory_uri().'/assets/images/seekers.png',        
    'public'                => true,
    'publicly_queryable'    => true,
    'show_ui'               => true,
    'query_var'             => true,
    'taxonomies'            => array('post_tag'),
    //'capability_type'       => 'post',
    'has_archive'           => true,
    'hierarchical'          => true,
    'supports'              => array( 'title', 'editor', 'thumbnail' ),
    'menu_position'         => 110,
    'show_in_menu'          => true,
    'show_in_nav_menus'     => false,        
);
register_post_type('seekers', $args);
}
add_action('init','seekers_cpt');

function seekers_cpt_taxonomy(){
    $args = array(
        'labels' => array(
            'name' => 'Seekers Types',
            'singular_name' => 'Seeker Type',
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'show_in_nav_menus' => false,
    );
    register_taxonomy('seekers-type', array('seekers'), $args);
}
add_action('init','seekers_cpt_taxonomy');

Solution

  • When you say I only want to display tags generated within seekers custom post. it sounds like the tag list would include tags from seekers-type or post_tags (since seekers is configured with the taxonomies configuration property set to post_tag), but only those added to the post itself (as opposed to being added on a Tags admin page).

    Unless you cache this tag list as tags are added to (and removed from) these posts, you could be looking at a loop through every seekers post, calling wp_get_post_terms() on each one (filtered by post_tag and seekers-type), and collecting the list of unique term names. That could be a time consuming loop depending on the number of posts.

    An approach with caching could take advantage of the 'save_post_seekers' action hook to update the cached tag list whenever a post is created or edited.