I try to query 5 posts in cpt 'vacancy'. Query is very complex. For first I need posts that belongs to custom taxonomy with ID 18 and sorted by 'post_modified', then - all other posts in cpt 'vacancy' also sorted by 'post_modified'. And all of this limited to 5 posts.
For now I use:
$query_hot = $wpdb->get_results("(SELECT * FROM wp_posts
WHERE post_type = 'vacancy' AND post_status = 'publish'
AND ID IN (SELECT object_id FROM wp_term_relationships WHERE
wp_term_relationships.term_taxonomy_id = 18)
ORDER BY post_modified DESC)
UNION
(SELECT * FROM wp_posts
WHERE post_type = 'vacancy' AND post_status = 'publish'
AND ID NOT IN (SELECT object_id FROM wp_term_relationships WHERE
wp_term_relationships.term_taxonomy_id = 18)
ORDER BY post_modified DESC) LIMIT 5");
It queries right posts, but not sorts by 'post_modified'. What I doing wrong?