0

I have a query with multiple orderby params in a custom page that I am having difficulty getting to work. The query is to return posts with either tag-AAA or tag-BBB tags. And I'd like to sort it based on DESC order by post_date but within the same pub day sort by post title in ASC order. I tried the following but it does not work. Whatever I set as the first argument in orderby sees to win the sort and the next appears to be not taken into account at all. Any assistance is appreciated.

$args = array(              
'tag' => 'tag-AAA,tag-BBB',     
array ('orderby' => array(   'title' => 'ASC', 'post_date' => 'DESC') ),
$query = new WP_Query($args);
1

Your code is invalid (syntax error, missing closing array and semi-colon), and incorrectly nested - if you were to properly indent your code, you'd have something like:

$args = array(              
    'tag' => 'tag-AAA,tag-BBB',     
    array (
        'orderby' => array(
            'title' => 'ASC',
            'post_date' => 'DESC',
        )
    ),
);

See how orderby isn't actually a property of $args? You need:

$args = array(              
    'tag'     => 'tag-AAA,tag-BBB', 
    'orderby' =>  array(
        'post_date' => 'DESC',
        'title'     => 'ASC',
    ),
);

I've also switched the order so that posts are ordered first by date, then by title.

  • Thanks I tried that but it is still not working. [Make WordPress Core #17065 Independent ASC/DESC in multiple ORDER BY statement] (core.trac.wordpress.org/ticket/17065) closed task (blessed) (fixed) – John May 22 '16 at 20:54
  • (last edit premature) Thanks, tried that but no luck. In my research I saw this discussing this exact matter: [core.trac.wordpress.org/ticket/17065] (Make WordPress Core #17065 Independent ASC/DESC in multiple ORDER BY statement) It says "closed" but comments note that DESC no work. A post by RossCurrie at bottom (~2 yrs ago) says "ordering by multiple attrs doesn't work well when date/post_date used, it's sorted as a datetime val. Even posts on same date are still ordered by time created. So if I want to sort by date,meta or date,title it just doesn't work" Did you get it to work? – John May 22 '16 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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