i try to run a custom post type query to match following criteria:
sort movies first by year in descending order,
after that (“inside” the year order) by title alphabetically.
desired output:
movie title A, 2006
movie title Z, 2006
…
movie title A, 1996
movie title Z, 1996
i use the following code:
$wp_query = new WP_Query();
$wp_query->query( array(
'post_type' => 'movies',
'distribution' => 'companyA',
'meta_key' => 'year',
'orderby' => 'meta_value_num title',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish',
));
i tried several things but i can only get this “inverse” result:
movie title A, 1996
movie title Z, 1996
…
movie title A, 2006
movie title Z, 2006
if i change DESC, ASC it only changes the title sorting. but i need to apply it to the year and not to the title.
is it the right way to use two orderby values? or do i have to use a meta_query or custom SQL ?
thx in advance!
Here is the resulting SQL Query from $GLOBALS['wp_query']->request
SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_term_relationships
ON ( wp_posts.id = wp_term_relationships.object_id )
INNER JOIN wp_postmeta
ON ( wp_posts.id = wp_postmeta.post_id )
WHERE 1 = 1
AND ( wp_term_relationships.term_taxonomy_id IN ( 24 ) )
AND wp_posts.post_type = ‘movies’
AND ( wp_posts.post_status = ‘publish’ )
AND ( wp_postmeta.meta_key = ‘year’ )
GROUP BY wp_posts.id
ORDER BY wp_postmeta.meta_value + 0,
wp_posts.post_title DESC