I had the problem by myself for numeric meta values and pointed out that it the order of the query is also important. For me the NOT EXISTS
query has to be the first one.
Example:
$query->set( 'orderby', 'meta_value''meta_value_num' );
$query->set( 'meta_query', [
'relation' => 'OR',
[ 'key' => 'your_meta_name', 'compare' => 'NOT EXISTS' ],
[
'key' => 'your_meta_name',
'orderby' => 'meta_value_num',
'type' => 'NUMBER',
'compare' => 'EXISTS',
],
] );
This way it orders every item, whichAlso important in order to get the right direction for numeric values is queried by the NOT EXISTS
bygeneral meta_value’orderby’
and uses the custom order by its numeric values if a valueto be set to EXISTS’meta_value_num’
.
Otherwise you have strange results for numeric values, e. g.:
1, 2, 20, 21, 3, 4, 5 …
Instead of:
1, 2, 3, 4, 5 … 20, 21