Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(
        array(
            'taxonomy'         => $taxfunc_tax_name,
            'field'            => 'id',
            'include_children' => 0,
            'terms'            => $term_id
        )
    ),
    'meta_query' => array(
        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

1 Answer 1

up vote 2 down vote accepted
+50

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
1  
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

 
discard

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.