What I'm after is creating a secondary (or default) orderby option for a custom query. Here is what I have:

$post_args = array(
    'post_type' => $taxfunc_post_type,
    'meta_key'  => '_mouldings_dimensions_height',
    'orderby'   => 'meta_value_num',
    'order'     => 'ASC',
    'paged'     => get_query_var('paged'),
    'tax_query' => array(
            'taxonomy'         => $taxfunc_tax_name,
            'field'            => 'id',
            'include_children' => 0,
            'terms'            => $term_id
    'meta_query' => array(
            'key' => 'special_profile',
            'value' => $special_logic_serialized

What I'd like to do is if _mouldings_dimensions_height is NULL, then default back to title. I did see Jan's answer on this, which does appear to be related, but I think I'm after the opposite. Ideally, I'd also be checking for just this query with query_var['post_type'] as the original questioner asked, but am not sure I am approaching this correctly. Any nudges in the right direction would be greatly apprecaited. Thanks!

Update After a bit more testing, it appears to list the oldest first as a fallback when the meta value is the same across multiple posts (NULL in this case) -- looking to change this behavior.

1 Answer 1


It's a quite simple revision to your current code. You can add multiple "orderby" values separated by a space.

'orderby'   => 'meta_value_num title',

This should keep the priority of sorting by your meta value, with a secondary sort of title.

  • 2
    I sort on a custom field (a year) and I want to set up secondary sorting on the publish date. I tried 'orderby' => 'meta_value date', but the addition of date breaks the primary sort order. Am I misunderstanding how this works?
    – Pieter
    Apr 20, 2013 at 19:13
  • Maybe post your query somewhere so we can take a look? It should by all means work.. Apr 21, 2013 at 20:10
  • I'm having the same problem as @Pieter refers. My query: 'orderby' => 'meta_value_num title', 'order' => 'DESC'. The DESC won't apply when the title sorting attribute is added.
    – dgilperez
    Aug 31, 2013 at 15:50
  • Aaccording to the examples in the Codex, it should work just fine. Try doing a parse_request hook and echoing out the SQL query itself. Maybe you will see where the issue lies! :) [codex.wordpress.org/Class_Reference/… Sep 3, 2013 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.