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

While I've been able to get this working for normal WP categories I've been unable to get it working for custom taxonomies.

I'd like to loop through each custom taxonomy (categories in my case) and produce a number of posts for each.

An example of the output would be:

Category 1

post from category one
post from category one

read more category one

Category 2

post from category two
post from category two

read more category two

Of course it would repeat through any available taxonomy for the custom post type.

share|improve this question
I don't completely understand your question. When you ask " loop through each custom taxonomy" do you mean " loop through the terms of each custom taxonomy?" Also, when you ask to "produce a number of posts" does "produce" mean insert new instances of a post type into the database or generate HTML for the post type? – MikeSchinkel Aug 14 '10 at 22:50
up vote 7 down vote accepted

I thought I would provide another answer as the above one is a little hacky, also I added another layer that gets all taxonomies for a post_type.

$post_type = 'post';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

foreach( $taxonomies as $taxonomy ) : 

    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy );

    foreach( $terms as $term ) : 

        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );

        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
            //Do you general query loop here  
        endwhile; endif;



It would be recommended to add each post found to a $post__not_in array, so you could pass that to the WP_Query to prevent duplicate posts coming through.

share|improve this answer

Are you looking for this ?

<?php query_posts(array('post_type' => 'post type name', 'Taxonomy slug' => $term->slug ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

How to create Custom Taxonomy loop

Hope it will help

share|improve this answer

Please check this example; create a own loop for your taxonomie. You can also use this in a foreach-loop to use all categories. Or you must create an own sql-query.

$taxonomies = get_the_term_list($post->ID, 'YOUR_TAXONOMIE', '', '', '');
$taxonomies = explode('>', $taxonomies);
$taxonomies = $taxonomies[1];
$myq = new WP_Query('your_taxonomie = '.$taxonomies); 
if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); // the loop ?>

            <?php the_title();?>
            <?php the_content();?>

<?php endwhile; else:?>

<?php endif;?>
share|improve this answer

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.