WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
And your question is? – Brady 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;
share|improve this answer
placed your code in archive.php, but it returned blank, for both domain.com/album-tags as well as domain.com/album-tags/test. What possibly goes wrong? – user6428 Jun 23 '11 at 14:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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