How to order and group custom WordPress posts by their custom taxonomies

Working on a project for a client, I needed to group an archive of custom post types by their categories. To complicate things further the categories were implemented as a custom taxonomy. On top of this the taxonomies had to be ordered alphabetically.

After a lot of experimenting with loops and different configurations of arguments, I came up with this solution.

[code]<?php
/*
Group and order custom posts by custom taxonomy
http://www.pauljoyceuk.com/codex/2013/how-to-group-wordpress-posts-by-custom-taxonomy

Change the following two lines to allow for your custom
taxonomy and the post type that uses it
*/
$taxonomy = array( "name" => "country" , "slug" => "country");
$custom_post_type = "pjuk_project";

if ( have_posts() )
the_post();
?>

<?php
// Query your specified taxonomy to get, in order, each category
$categories = get_terms($customTaxonomy[‘name’], ‘orderby=title’);
foreach( $categories as $category ) {
?>

<div id="content" class="<?php echo strtolower($category->name); ?>">

<!– Output the category name as a H1, linking to its specified URL –>
<h1 class="page-title"><span>
<a href=’/<?php echo $taxonomy[‘slug’]; ?>/<?php echo $category->slug; ?>’>
<?php echo $category->name; ?>
</a>
</span></h1>

<?php
// Setup query to return each custom post within this taxonomy category
$o_queried_posts = get_posts(array(
‘nopaging’ => true,
‘post_type’ => $custom_post_type,
‘taxonomy’ => $category->taxonomy,
‘term’ => $category->slug,
));
?>

<div id=’archive-content’>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $o_post) {
setup_postdata($o_post); // Helps to format custom query results for using Template tags. See links in article for further explanation
?>

<div id="post-<?php the_ID(); ?>" <?php post_class($custom_post_type); ?>>

<!– Layout the post type excerpt as you see fit by modifying the next block –>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr( ‘Permalink to %s’ ), the_title_attribute( ‘echo=0’ ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="archive-excerpt">
<?php the_excerpt(); ?>
</div><!– .archive-excerpt –>

</div><!– #post –>
<?php } // foreach($o_queried_posts as $o_post) ?>
</div> <!– archive-content –>
</div> <!– #content –>
<?php } // foreach( $categories as $category ) ?>[/code]

Ideally you would try to combine the queries into each other, so you weren’t looping through and hitting the database for each iteration of the loop. Therefore I’m not 100% confident how this solution would scale for huge data sets, but I only had a handful of taxonomies options, each with only a few projects in each.

Here are a bunch of useful pages I used to build this query if you care to delve deeper into the internal workings: Querying and Ordering WordPress posts, setup_postdata() function and similar problem by another WP engineer.

Hope it helps,

Leave a Reply

Your email address will not be published.