WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
Dope - appreciate it @Eric. I'll accept the bounty as soon as the timer hits (gotta wait 21 hours) – Zach Aug 31 '12 at 18:01
No problem Zach! :) – Eric Holmes Sep 5 '12 at 13:15
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 '13 at 19:13
Maybe post your query somewhere so we can take a look? It should by all means work.. – Eric Holmes Apr 21 '13 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 '13 at 15:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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