I hope i can write a clear question since i got confused trying to solve this...

I got a custom post type... that custom post type has taxomony (category like)
and i want to return a structure like this in a page that show all posts of
that post type..

--------------------------------------------- DESIRED STURCTURE

taxomony category name (Example: new york)
- post one (assosicated with new york category)
- post two (assosicated with new york category)
- post three (assosicated with new york category)

taxomony category name (Example: Washington DC)
- post one (assosicated with Washington DC category)
- post two (assosicated with Washington DC category)
- post three (assosicated with Washington DC category)
- post four (assosicated with Washington DC category)
- post Five (assosicated with Washington DC category)

taxomony category name (Example: SomeCity)
- post one (assosicated with SomeCity category)
- post two (assosicated with SomeCity category)

This is the code i have now:

        <div class="pageContent portfolioPage">

            <!--=STR== PAGE TITLE ===-->
            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>   
                <h1 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h1>
                <?php the_content(); ?>
            <?php endwhile; endif; ?>
            <!--=END== PAGE TITLE ===-->

            <!--=STR== OUR CLIENTS ===-->                   
                    'post_type' => 'vacationrental',
                    'showposts' => 100
                ) );  
            $loop = new WP_Query( array( 
            'post_type' => 'vacationrental',
            'post_per_page' => 100,
            'orderby' => 'date',
            'order' => 'ASC'


            <?php while ($loop->have_posts()) : $loop->the_post(); ?>
                <div class="vacationRentalBox">
                    <div class="vacationRentalBox-inner">

                        $image_id = get_post_thumbnail_id();  
                        $image_url = wp_get_attachment_image_src($image_id,'large');  
                        $image_url = $image_url[0];  
                        // $googleMapsUrl = get_post_meta( $post->ID, 'vacationrental_map', true ); // GOOGLE MAPS URL

                        <div class="vacationRentalBar">
                            <div class="vacationRentalName"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> 

                            <div class="thumbsingle">
                            <a title="<?php the_title(); ?>" href="<?php echo $image_url; ?>" class="lightbox"><?php the_post_thumbnail('testimonialPage'); ?></a>

                            <div class="vacationRentalDescription"><?php dynamic_excerpt(315); ?></div> 

                            <div class="vacationRentalExtraInfo">
                                <div class="vacationRentalReadMore"><a href="<?php the_permalink(); ?>" rel="nofollow"><?php _e('Read More &rarr;' ,'sagive'); ?></a></div>                             
                            <br style="clear: both;" />


            <?php endwhile;?>
            <!--=END== OUR CLIENTS ===-->                           

            <br style="clear: both;" />

I managed to get the list of taxomonies categories using this:

$terms = get_terms("locations");
$count = count($terms);
if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
    echo "<li>" . $term->name . "</li>";

    echo "</ul>";

But i dont know how to integrate that into the loop / structure Would really apreaciate your help since i am stuck...

2 Answers 2


If I follow your question correctly you could use a nested query loop, that is looping through your taxonomy terms and then doing a WP_Query loop for each one.

There is a more complicated approach using custom SQL and a filter but the following example is what I would go for:

$terms = get_terms("locations");
$count = count($terms);
if ( $count > 0 ){
    foreach ( $terms as $term ) {
        echo '<h2>' . $term->name . '</h2>';
        echo '<ul>';
        $loop = new WP_Query( array( 
            'post_type' => 'vacationrental',
            'post_per_page' => 100,
            'orderby' => 'date',
            'order' => 'ASC',
            'tax_query' => array(
                    'taxonomy' => 'locations',
                    'field' => 'id',
                    'terms' => $term->term_id
        // the loop
        while ($loop->have_posts()) : $loop->the_post();
            // do loop content
            echo '<li>' . get_the_title() . '</li>';
        // reset $post so that the rest of the template is in the original context
        echo '</ul>';
  • Don't you want to reset before the endwhile? Otherwise I agree, would do it the same way, +1
    – Noel Tock
    Jan 9, 2012 at 15:47
  • Yeah I guess it wouldn't hurt - the post stuff is getting overridden in the $loop->the_post(); bit though so I left it outside. I've not tested it mind you. Jan 9, 2012 at 16:01
  • 10x a lot (really!) :) - aprroved and up'd 1 - have a gr8 day... i need to wait hour but you got the bounty
    – Sagive
    Jan 9, 2012 at 21:15
  • @SagiveSEO cheers :) Jan 13, 2012 at 17:59

ok, this is old and you have a solution, but i was trying to do the same thing. here is how i ended up doing it w/ SQL voodoo patched together from a couple of places, but mostly:

Using wp_query is it possible to orderby taxonomy? and

how to group custom post type posts by custom taxonomy terms

first the query voodoo:

function wpa_38075( $clauses, $wp_query ) {
    global $wpdb;

    if( !is_post_type_archive( 'vacationrental' )) return $clauses;

        //join term_relationships to posts, and term_relationships to term_taxonomy and term_taxonomy to terms
        $clauses['join'] .= "LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)" ;

        //look for posts with and without a subject term
        $clauses['where'] .= " AND (taxonomy = 'locations' OR taxonomy IS NULL)";

        $clauses['groupby'] = "object_id";

        //remove limits
        $clauses['limits'] = "";

        //group posts by term name
        $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";

    return $clauses;

add_filter( 'posts_clauses', 'wpa_38075', 10, 2 );

and then in your template:


$prev = ''; //not set yet

if (have_posts()) :

    echo "<ul>";

while ( have_posts() ) : the_post();  

    $subhead =  array_shift(wp_get_post_terms(get_the_ID(), 'subject', array("fields" => "names")));
    $subhead = $subhead ? $subhead : __('Uncategorized','yourtextdomain');

    if($subhead != $prev) { ?>
        <h3 class="subhead"><?php echo $subhead;?></h3>
    <?php } ?>

    <li><a href="<?php echo get_permalink();?>" title="<?php echo __('Permalink to', 'peterwade') . ' ' . get_the_title();?>"><?php the_title();?></a></li>


    $prev = $subhead; //cache the previous tax term

echo "</ul>";
  • cool @helgatheviking - looks goods... would try it and thanks for sharing
    – Sagive
    Jul 18, 2012 at 5:38
  • sure thing. i hope it helps the next person who comes along with the same issue. Jul 18, 2012 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.