WP_Query() ‘orderby’ => ‘title’ not working

So I have this query

$args = array(
    'post_type' => 'course', // custom post type
    'post_status'   => 'publish',
    'posts_per_page' => -1,
    'orderby'   => 'title'
);

$courses = new WP_Query($args);

This gives me what I want, but the orderby statement is being ignored. When I dump the $courses->request I get this

Read More

‘SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type
= ‘course’ AND ((wp_posts.post_status = ‘publish’)) ORDER BY wp_posts.menu_order ASC ‘

It is defaulting the orderby to menu_order instead of title. What’s going on here?

Related posts

2 comments

  1. I was facing the same issue and I solved it using ‘post_title’ instead of ‘title’.

    $args = array(
        'post_type' => 'course', // custom post type
        'post_status'   => 'publish',
        'posts_per_page' => -1,
        'orderby'   => 'post_title' // change here
    );
    
    $courses = new WP_Query($args);
    
  2. check whether you have used parse_query or pre_get_posts
    hook somewhere in the website

    add_action( 'pre_get_posts', 'function_name' );
    add_filter( 'parse_query',  'function_name' );
    

Comments are closed.