wp query - Orderby ASC changes to DESC in WP_Query

I am trying to get posts in Ascending order using WP_Query,$args= array('date_query' => array(array('

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

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

Share Improve this question edited May 25, 2019 at 21:01 admcfajn 1,3262 gold badges13 silver badges30 bronze badges asked Jul 4, 2017 at 22:04 Abdul WaheedAbdul Waheed 131 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745464095a4628838.html

相关推荐

  • wp query - Orderby ASC changes to DESC in WP_Query

    I am trying to get posts in Ascending order using WP_Query,$args= array('date_query' => array(array('

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信