Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress WP_Query delete ORDER BY wp_posts.menu_order

Tags:

wordpress

I want to get posts in the order that I request.

Luckly, there was a query argument -> orderby = 'post_name__in'.

But, I get posts in other order.

And I see the query's request.

Here

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts  
WHERE 1=1  
AND wp_posts.post_name 
IN (
  'post title 1',
  'post title 2',
  'post title 3',
  ) 
AND wp_posts.post_type = 'post' 
AND ((wp_posts.post_status = 'publish')) 
ORDER BY wp_posts.menu_order,
FIELD( 
  wp_posts.post_name, 
  'post title 1',
  'post title 2',
  'post title 3',
  ) 
LIMIT 0, 9

I find that ORDER BY wp_posts.menu_order was a problem.

If I delete it and then Query on phpmyadmin, It works well.

I get posts in the order that I want.

So, how can I delete that ORDER BY wp_posts.menu_order by using hook & filter or some other wordpressful way ?

like image 383
Byeongin Yoon Avatar asked Jun 26 '26 09:06

Byeongin Yoon


1 Answers

I found answer!

the wp_posts.menu_order was added because of Post Types Order plugin.

In this plugin,

add_filter('posts_orderby', array($this, 'posts_orderby'), 99, 2);
~
~
~
//check for ignore_custom_sort
if (isset($query->query_vars['ignore_custom_sort']) && $query>query_vars['ignore_custom_sort'] === TRUE)
    return $orderBy;
if(trim($orderBy) == '')
  $orderBy = "{$wpdb->posts}.menu_order " . $order;
else
  $orderBy = "{$wpdb->posts}.menu_order". $order .", " . $orderBy;
~
~
return $orderBy;

So, I add $args['ignore_custom_sort'] = true to my query argument.

And this works!

like image 144
Byeongin Yoon Avatar answered Jun 28 '26 22:06

Byeongin Yoon