• I have made a FAQs accordion using a Custom Post Type. The CPT is named ‘faq’ with a taxonomy of ‘category’. ‘Category’ has eight or so different terms. What I’m trying to do is list out every term and then every post that pertains to that term. What I have lists every term in a h2 element nicely. But, it then lists every post in the FAQ under every term. What am I doing wrong here?

    <?// for a given post type, return all except 'uncategorized'
      $post_type = 'faq';
      $tax = 'category';
      $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '1'));
      if ($tax_terms) {
        foreach ($tax_terms as $tax_term) {
          $args = array(
            'post_type' => $post_type,
    	$tax => $tax_term->slug,
    	'post_status' => 'publish',
    	'posts_per_page' => - 1,
    	'orderby' => 'title',
    	'order' => 'ASC',
    	'caller_get_posts' => 1
          ); // END $args
          $my_query = null;
          $my_query = new WP_Query($args);
         if ($my_query->have_posts()) {
           echo '<div class="aro"><div class="wrap"><h2>' . $tax_term->name . '</h2></div><div class="details"><div class="wrap">';
           while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <div class="info">
    	   <h3><?php the_title(); ?></h3>
    	   <?php the_content(); ?>
    	 </div>
    <?php
    	endwhile; } // END if have_posts loop
          echo '</div></div></div>'; // Close 'details', 'wrap', & 'aro' DIVs
        wp_reset_query(); } // END foreach $tax_terms
      } // END if $tax_terms
    ?>

    Example of what I want it to do:

    Term 1
    Post 1 Title of Term 1
    Post 1 Content of Term 1
    Post 2 Title of Term 1
    Post 2 Content of Term 1
    Term 2
    Post 1 Title of Term 2
    Post 1 Content of Term 2
    Post 2 Title of Term 2
    Post 2 Content of Term 2

    And so on and so forth.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think the problem is in this line:

    $tax => $tax_term->slug,

    $tax is set to ‘category’ which is not a valid parameter. It needs to be ‘category_name’.

    Try using a tax_query instead:

    $args = array(
       'post_type' => $post_type,
       'tax_query' => array(
          array(
             'taxonomy' => $tax,
             'field' => 'slug',
             'terms' => $tax_term->slug
          )
       ),
       'post_status' => 'publish',
       'posts_per_page' => - 1,
       'orderby' => 'title',
       'order' => 'ASC',
       'caller_get_posts' => 1
    ); // END $args

    Try this..

    [Moderator Note: Damaged code removed. – Post code & markup between backticks or use the code button. Your posted code was damaged by the forum’s parser.]

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Listing Custom Post Types by Terms’ is closed to new replies.