title: Show Categories and Post Titles for Custom Post Type Archive - Wordpress
body: <p>I'm trying to set up a custom post archive page where the categories for that custom post are displayed, and the posts within that category displayed underneath.</p>
  
  <p>The page in question is in development at <a href="http://vinnypeculiar.com/wp/lyrics" rel="nofollow">http://vinnypeculiar.com/wp/lyrics</a></p>
  
  <p>The post type is called Lyrics, and the categories within that type are called Albums (individually category listed as 'album').</p>
  
  <p>So the archive page should look like:</p>
  
  <p>ALBUM NAME #1</p>
  
  <ul>
  <li>Song #1 from ALBUM</li>
  <li>Song #2 from ALBUM</li>
  </ul>
  
  <p>ALBUM NAME #2</p>
  
  <ul>
  <li>Song #1 from ALBUM</li>
  <li>Song #2 from ALBUM</li>
  </ul>
  
  <p>At the moment, with the code I'm using, it successfully shows the album names, but it shows EVERY post under each category, which is not correct.</p>
  
  <p>The code I'm using almost works, but I think it just needs to be tweaked somehow:</p>
  
  <pre><code>&lt;?php
  /*Template Name: Lyrics*/
  get_header();?&gt;
  
  &lt;div id="content"&gt;
  
  &lt;h2&gt;Lyrics &amp; Poems&lt;/h2&gt;
  
  &lt;?php
  //get all categories then display all posts in each term
  $taxonomy = 'album';
  $param_type = 'album__in';
  $term_args=array(
    'orderby' =&gt; 'title',
    'order' =&gt; 'ASC'
  );
  $terms = get_terms($taxonomy,$term_args);
  if ($terms) {
    foreach( $terms as $term ) {
      $args=array(
        "$param_type" =&gt; array($term-&gt;term_id),
        'post_type' =&gt; 'lyrics',
      'category' =&gt; '%album%',
        'post_status' =&gt; 'publish',
        'posts_per_page' =&gt; -1,
        'caller_get_posts'=&gt; 1,
    'orderby' =&gt; 'title',
    'order' =&gt; 'ASC'
        );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query-&gt;have_posts() ) {  ?&gt;
        &lt;div class="category section"&gt;
          &lt;h3&gt;&lt;?php echo ''.$term-&gt;name;?&gt;&lt;/h3&gt;
          &lt;ul&gt;
          &lt;?php
        while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post(); ?&gt;
          &lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
         &lt;?php
        endwhile;
        ?&gt;
        &lt;/ul&gt;
        &lt;/div&gt;
   &lt;?php
      }
    }
  }
  wp_reset_query();  // Restore global post data stomped by the_post().
  ?&gt;
  
  
  &lt;/div&gt;
  
  
  &lt;?php get_footer();?&gt;
  </code></pre>