Actually, I don’t think you need that mt1.post_id IS NOT NULL DESC
sorting, hence you also do not need to use the posts_orderby
hook. You can just use the orderby
parameter in WP_Query
like so:
Also note that, in the first meta query clause named/keyed start_date_value
, you should set the type
arg to DATE
(like you can see below) so that MySQL/MariaDB casts the meta value as a date and then properly sorts the posts by date.
$meta_query = [
'relation' => 'OR',
'start_date_value' => [
'key' => 'start_date',
'type' => 'DATE', // set the 'type' to DATE
],
'start_date' => [
'key' => 'start_date',
'compare' => 'NOT EXISTS',
],
];
$query->set( 'meta_query', $meta_query );
// Use an array with the orderby.
$query->set( 'orderby', [
'start_date_value' => 'DESC', // sorts by the meta start_date
'post_date' => 'DESC', // then sorts by the post date
] );
And as I said in the comments, you should understand that when sorting by multiple columns like above where the columns are the start_date
meta and the post date, values that are already sorted in the previous column won’t be sorted again, unless for values that are equal like the 20200211
(start_date
) below. You can try a demo on DB Fiddle which outputs this:
---
**Query #1 - before sorting **
| Post Title | start_date | Post Date |
| ------------- | ---------- | ---------------------- |
| News 1 | 20200211 | March 12th, 4:00AM |
| News 2 | 20200926 | September 19th, 5:36AM |
| News 3 | | June 5th, 8:07PM |
| Publication 1 | 20201021 | October 16th, 5:40PM |
| Publication 2 | 20200211 | March 12th, 11:43AM |
---
**Query #2 - after sorting by the meta start_date **
| Post Title | start_date | Post Date |
| ------------- | ---------- | ---------------------- |
| Publication 1 | 20201021 | October 16th, 5:40PM |
| News 2 | 20200926 | September 19th, 5:36AM |
| News 1 | 20200211 | March 12th, 4:00AM |
| Publication 2 | 20200211 | March 12th, 11:43AM |
| News 3 | | June 5th, 8:07PM |
---
**Query #3 - after sorting by the meta start_date, AND THEN the post date **
| Post Title | start_date | Post Date |
| ------------- | ---------- | ---------------------- |
| Publication 1 | 20201021 | October 16th, 5:40PM |
| News 2 | 20200926 | September 19th, 5:36AM |
| Publication 2 | 20200211 | March 12th, 11:43AM |
| News 1 | 20200211 | March 12th, 4:00AM |
| News 3 | | June 5th, 8:07PM |
Note though, the demo doesn’t use the full WordPress database schema, but I just wanted to demonstrate the multiple columns sorting. And I hope that demo and this answer helps you? 🙂