Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

3 Answers 3

up vote 6 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;

    endforeach;

endforeach;

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.

<?php
$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

 
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.