WordPress WP_Query orderby being overwritten

$args = array(
    'post_type'      => 'mcm_company',
    'orderby'        => 'title',
    'order'          => 'ASC',
    'posts_per_page' => '1000',
    'post_status'    => 'publish',
);
$r = array();       
$the_query = new WP_Query( $args );

I’m expected to see the results get returned in alphabetical order, but it’s not. The final query is

[request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  WHERE 1=1  AND wp_posts.post_type="mcm_company" AND ((wp_posts.post_status="publish"))  ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC LIMIT 0, 1000

which you can see is getting the wrong ORDER BY. I’m assuming something is overwriting the orderby… is there a way to force the order to title?


The issue was from the Post Types Order plugin, with the Auto Sort option enabled

1 Answer
1

You can also add the following code before running new WP_Query:

remove_all_filters('posts_orderby');

I believe this is self-explanatory.

Leave a Comment