1

I am trying to get posts in Ascending order using WP_Query,

$args  = array(
    'date_query' => array(
        array(
            'year' => $ppy,
            'orderby' => 'post_date',
            'order' => 'ASC',
        ),
    ),
);
$query = new WP_Query( $args );

But I am getting posts in descending order, I var_dumped the query and noticed that order is still DESC,

[request] => SELECT SQL_CALC_FOUND_ROWS wpqk_posts.ID FROM wpqk_posts WHERE 1=1 AND ( YEAR( wpqk_posts.post_date ) = 2017 ) AND wpqk_posts.post_type = 'post' AND ( wpqk_posts.post_status = 'publish' OR wpqk_posts.post_status = 'acf-disabled' OR wpqk_posts.post_status = 'private' ) ORDER BY wpqk_posts.post_date DESC LIMIT 0, 10

1

You've made "orderby" and "order" part of the date_query sub-array. "Order" parameters belong to the main parameters array.

I can't vouch for the part of your code that concerns the year and the above-undefined variable $ppy, but if you want the posts from a specified year in ascending order by 'post_date' (which is the default), you'd try:

$args = array( 
        'date_query' => array(
                     'year' => $ppy,
         ),
         'order'   => 'ASC',
);

You can leave off 'post_date', since it's the default, but it doesn't hurt to specify if you've got a lot else going on that may potentially change the query.

  • Thank you very much dear, feeling very very stupid at the moment... :) – Abdul Waheed Jul 5 '17 at 21:32
  • You're quite welcome... that seems to me like an easy one to get wrong if you haven't used it often or didn't read the Codex v closely... and why shouldn't order/orderby sub-parameters work!? So: no shame! – CK MacLeod Jul 5 '17 at 21:34

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.