Actually, it IS possible to get the main query to sort by taxonomy... it just isn't really pretty. First and foremost: this will work by manipulating the query that is executed by WordPress. If you don't know your way around the WordPress database, i would probably not recommend doing this because you could f up your website. But let's get into the meat of things, okay?
Step 1: Add the Filter
First and foremost, i am assuming that you want to order the "releases" on the "release"-Post-Type-Archive.
To change the SQL-Query performed by WordPress directly, you can use the filter "posts_clauses", which you can get more information on here.
To add the filter only on the archive of the custom post type, we will use the pre_get_posts
filter like this:
add_action('pre_get_posts','setup_my_query_interception');
function setup_my_query_interception($query){
if( !is_admin() && $query->is_post_type_archive( 'release' ) && $query->is_main_query() ){
add_filter( 'posts_clauses', 'order_by_taxonomy_intercept_query_clauses', 20, 1 );
}
}
The Database Query Pieces will now be sent to our function order_by_taxonomy_intercept_query_clauses
(which will be in step 2) before being executed.
Step 2: Change the query
We have to extend the MySQL Query to include the term and taxonomy tables. We use the Filter-Function order_by_taxonomy_intercept_query_clauses
for this.
function order_by_taxonomy_intercept_query_clauses($pieces){
global $wpdb;
$pieces['join'].=" LEFT JOIN $wpdb->term_relationships trt ON ($wpdb->posts.ID = trt.object_id) LEFT JOIN $wpdb->term_taxonomy ttt ON (trt.term_taxonomy_id = ttt.term_taxonomy_id) INNER JOIN $wpdb->terms termts ON (termts.term_id = ttt.term_id)";
$pieces['where'].=" AND ttt.taxonomy = 'release-year'";
$pieces['orderby']="termts.name ASC, ".$pieces['orderby'];
//Remove the filter so it only runs once in our main query
remove_filter('posts_clauses', 'order_by_taxonomy_intercept_query_clauses');
return $pieces;
}
Step 3: Success!
Your Releases will now be ordered by release-year.
Happy Coding!
'orderby' => 'meta_value_num'
. The ASC is actually a separate arg valueorder
.'orderby' => 'release-year'
andmeta_key' => 'release-year', 'orderby' => 'meta_value_num'
. The first orders them by title. The second shows no results.