1
$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

  • I haven't written any, but it's possible maybe another plugin is.. Is there a way to force the orderby, or do a final filter override? or a way to see where it might be coming from? – cire Oct 19 '17 at 20:07
  • I was hoping not to have to, but I will try that next. thanks – cire Oct 19 '17 at 20:08
  • I just try your code on a clean WordPress version 4.8.2 and I obtain '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.post_title ASC LIMIT 0, 1000' – mmm Oct 19 '17 at 20:19
  • Narrowed it down, it was because I'm using the Post Types Order plugin, and had the Auto Sort option enabled. – cire Oct 19 '17 at 20:31
0

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

remove_all_filters('posts_orderby');

I believe this is self-explanatory.

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.