Wordpress - List all posts in custom post type by taxonomy
Try this
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
endwhile;
}
}
We get all the terms of a taxonomy, loop through them, and fire off a title link to each post that belongs to that term. If you need to reorder the taxonomy terms, you can do so with a plugin pretty easily. Reorder Taxonomy, I believe. But pay attention that this plugin adds(!) another column to your table on activation and does not remove it upon deactivation!
Not a particularly elegant solution but you can create multiple queries each for the specific terms and then output them. Hopefully someone can come up with a nicer way of automatically pulling the terms to modify the output/sorting. But this would get you going.
<?php
//First Query for Posts matching term1
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'taxonomy_1',
'field' => 'slug',
'terms' => array( 'term1' )
),
),
'post_type' => 'my-post-type'
);
$query = new WP_Query( $args );
if ( have_posts() ) {
$term = $query->queried_object;
echo 'All posts found in ' . $term->name;
while ( have_posts() ) : the_post();
//Output what you want
the_title();
the_content();
endwhile;
}
//RESET YOUR QUERY VARS
wp_reset_query();
//Second Query for term2
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'taxonomy_1',
'field' => 'slug',
'terms' => array( 'term2' )
),
),
'post_type' => 'my-post-type'
);
$query = new WP_Query( $args );
if ( have_posts() ) {
$term = $query->queried_object;
echo 'All posts found in ' . $term->name;
while ( have_posts() ) : the_post();
//Output what you want
the_title();
the_content();
endwhile;
}