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 would like to create one loop that lists the custom posts for each taxonomy term:

Term A: Item Item Item

Term B: Item Item Item

I would like this to be totally dynamic so if I add a new term it automatically appears. I've seen examples where the taxonomy terms are explicity in the code but I am looking for something lower maintenance and more elegant.

share|improve this question
up vote 1 down vote accepted

Try:

$tt = get_terms('my_custom_taxonomy', array(
    // You can stick in orderby, order, exclude, child_of, etc. params here.
));

foreach ($tt as $term) :
    // Output term name
    print $term->name.  ": ";

    $q = new WP_Query(array(
        'post_type' => 'custom_post_type_i_use',
        'post_status' => 'publish',
        'posts_per_page' => -1, // = all of 'em
        'tax_query' => array(
            'taxonomy' => $term->taxonomy,
            'terms' => array( $term->term_id ),
            'field' => 'term_id',
        ),
    ));

    $first = true;
    foreach ($q->posts as $item) :
        // ... now do something with $item, for example: ...
        if ($first) : $first = false; else : print ", "; endif;
        print '<a href="'.get_permalink($item->ID).'">'
          .$item->post_title.'</a>';
    endforeach;
endforeach;

Does that do more or less what you needed?

share|improve this answer
    
That does exactly what I needed, thank you – Diane Nov 1 '11 at 0:17

Your Answer

 
discard

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.