Get posts grouped by categories (taxonomies) and standalone posts
<?php
//List custom posts grouped by categories
$customtaxname = 'category_itineraries';
$customtax_cats = get_categories( array('taxonomy' => $customtaxname) );
foreach($customtax_cats as $customtax_cat) {
$customtax_cats_query = new WP_Query( array( $customtaxname => $customtax_cat->slug ) );
echo '<h1>'.$customtax_cat->name.'</h1>';
while ($customtax_cats_query->have_posts()) : $customtax_cats_query->the_post();
Starkers_Utilities::get_template_parts( array( 'parts/post' ) );
endwhile;
wp_reset_postdata();
};
//List posts without categories
$alltax_ids = get_terms( $customtaxname, array('fields' => 'ids', 'get' => 'all') );
$posttype_by_tax = get_taxonomy($customtaxname);
$nocatqueryargs = array(
'post_type' => $posttype_by_tax->object_type[0],
'tax_query' => array(
array(
'taxonomy' => $customtaxname,
'field' => 'id',
'terms' => $alltax_ids,
'operator' => 'NOT IN',
)
)
);
$nocatquery = new WP_Query( $nocatqueryargs );
if ($nocatquery->have_posts()) :
echo '<h1>'.__('Itineraries not in categories','anno').'</h1>';
while ($nocatquery->have_posts()) : $nocatquery->the_post();
Starkers_Utilities::get_template_parts( array( 'parts/post' ) );
endwhile;
endif;
wp_reset_postdata();
?>