• Resolved pswpsw

    (@pswpsw)


    Hello,

    In WordPress 5.9, I cannot get WP_Query to order by any column other than the Post Date.

    Given the following code:

    
    $args = array(
        'orderby' => 'title',
        'order'   => 'DESC',
    );
    $query = new WP_Query( $args );
    

    The returned query request is as follows:

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

    Note order by statement, ‘ORDER BY wp_posts.post_date ASC’.

    It doesn’t matter what kind of query I throw at WP_Query it will ONLY order by the post date!

    Please help, I cannot understand how this could be so.

    Paul S.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi! Your code looks fine, and I tested it on a fresh 5.9 install. I was able to sort by several params (like title or rand), so I’m wondering if that request could belong to a different query.

    Could you some extra arguments to the query to be sure?

    Thread Starter pswpsw

    (@pswpsw)

    It looks like I found the culprit. It seems that at some point I’d used a:

    ‘order_by’ => ‘title’

    instead of:

    ‘orderby’ => ‘title’

    Is it just me, or this naming convention inconsistent?

    Thanks.

    Oh, good call! I’m not sure why orderby doesn’t use an underscore, but it indeed looks inconsistent with other parameters. Maybe it’s just a legacy decision?

    If you don’t need more help, could you please mark this topic as resolved?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query will ONLY order by post_date column’ is closed to new replies.