You should filter the default $query
via callback hooked into pre_get_posts
:
function wpse108983_filter_pre_get_posts( $query ) {
// Make sure we're targeting
// the main loop query, and
// only in the archive context
if ( $query->is_main_query() && is_archive() ) {
// Add your query modifications here
// For example, to sort by date:
$query->set( 'orderby', 'date' );
}
}
add_action( 'pre_get_posts', 'wpse108983_filter_pre_get_posts' );
If you need to output multiple loops in order to group by taxonomy, you will, instead of the above approach, want to output custom loops via new WP_Query()
.
Assuming that you already know how to query terms for your taxonomy foobar
, and have them in an array, $term_array
:
// Loop through each term
foreach ( $term_array as $single_term ) {
// Generate the term query
$term_query = new WP_Query(
'foobar' => $single_term,
'orderby' => 'date'
);
// Output the term query loop
if ( $term_query->have_posts() ) :
while ( $term_query->have_posts() ) :
$term_query->the_post();
// Custom loop markup goes here
// Use normal loop template tags, such as
// the_content(), the_permalink(), etc
endwhile;
endif;
// Important; don't forget this!
wp_reset_postdata();
}