I'm a newbie in custom post type. and so far I've been able to customize list of post (archive.php or archive-xxx.php), and link to individual post (single.php or single-xxx.php). All these works good.

However, when retrieving tag list or list of post of a specific tag, it uses archive.php and list all posts under the specific custom post type.

[question] How can I fetch list of all tags for this post_type, with posts underneath each tag?

   post 1
   post 2

  • And your question is? – Scott Jun 23 '11 at 12:07
  • @Brady my post was trimmed. Just added the question above. My archive.php has only simple WP_Query for all posts under post_type album, and a failing code trying to retrieve all tags under post_type album. When clicking on the tag link, such as 'domain.com/album-tags/test' returns the list of all posts for the post_type album, similar to 'domain.com/album' – user6428 Jun 23 '11 at 12:35

Try this:

global $post;
$tmp_post = $post;

//First get the list of terms

$terms = get_terms("post_tag");

//then loop over the list and get the posts of each term
echo "<ul>";
foreach ( $terms as $term ) {
    $args = array('posts_per_page' => -1, 'tag' =>  $term->slug, 'post_type' => 'album');
    $myposts = get_posts( $args );
    if (count($myposts) > 0){
        echo "<li>" .$term->name . "<ul>";
        foreach( $myposts as $post ){ 
            echo '<li><a href="'.the_permalink(); .'">'.the_title().'</a></li>';
        echo "</ul></li>";
echo "</ul>";
$post = $tmp_post;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.