I'm trying to get list of custom posts ordered by numeric meta_key first if such key exist, and than if there is no key, continue order by title.
I mean:
- AObject (object_order => 1)
- FObject (object_order => 2)
- BObject (object_order => 3)
- AObject (object_order => NULL)
- BObject (object_order => NULL)
- CObject (object_order => NULL) ... and so on.
I have such code:
$properties_for_map = array(
'post_type' => 'property',
'posts_per_page' => -1,
'meta_key' => 'object_order',
'orderby' => 'meta_value_num title',
'order' => 'ASC',
'tax_query' => $tax_query
);
But it only showing post with meta key.. I tried to find an answer on the site, and found some examples, but did not quite understand them. Maybe someone can help me and explain this method.
UPDATE 1: It can be done like this?
$properties_for_map = array(
'post_type' => 'property',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => object_order,
'compare' => 'EXISTS',
),
array(
array(
'key' => object_order,
'compare' => 'NOT EXISTS',
),
),
),
'orderby' => array( 'meta_value_num' => 'ASC', 'title' => 'ASC' ),
'tax_query' => $tax_query
);