WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#15388 closed defect (bug) (worksforme)

get_posts does not order by menu_order

Reported by: ggus Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.4
Component: Query Keywords: menu_order get_posts orderby
Focuses: Cc:

Description

I'm writing a plugin that makes use of custom post_types. The plugin (in the back-end) aims to manage a group of custom posts children of the actually edited custom post. I manage the ordering via the menu_order.

a statement like

get_posts('post_type=whatever&post_parent='.$post->ID.'$orderby=menu_order&order=ASC')

does not order by menu_order, instead it returns the elements ordered by the default order (id?)

As reported in
http://wordpress.org/support/topic/get_posts-not-using-orderbymenu_order#post-1006470
and also as stated in this bug fix:
http://core.trac.wordpress.org/ticket/6731
there is in the core file query.php at line 2147 (WP Version 3.0.1)

switch ( $orderby ) {
  case 'menu_order':
  break;

In this way every request to order by menu_order gets ignored.

It seems that query_posts function is unaffected by this, so

 query_posts(array('post_type' => 'whatever', 'post_parent' => $post->ID , 'order' => 'ASC', 'orderby' => 'menu_order'));

does work, but I fear there are some implications in using query_posts instead of get_posts.

There is a solution proposed in the same page
http://wordpress.org/support/topic/get_posts-not-using-orderbymenu_order#post-1006470

that fixes the bug by changing the code to

case 'menu_order':
  $orderby = "$wpdb->posts.menu_order";
  break;

Change History (9)

comment:1 ryan3 years ago

This request:

get_posts('post_type=page&orderby=menu_order&order=DESC');

Produces this query:

SELECT wp_trunk_posts.* FROM wp_trunk_posts WHERE 1=1 AND wp_trunk_posts.post_type = 'page' AND (wp_trunk_posts.post_status = 'publish') ORDER BY menu_order ASC LIMIT 0, 5

menu_order is properly represented in the query and the pages are ordered as expected. That case statement is unnecessary but doesn't seem to hurt anything. Further, the Pages admin screen does orderby='menu_order title' just fine.

comment:2 in reply to: ↑ description SergeyBiryukov3 years ago

Replying to ggus:

get_posts('post_type=whatever&post_parent='.$post->ID.'$orderby=menu_order&order=ASC')

Shouldn't it be &orderby=menu_order instead of $orderby=menu_order?

comment:3 johnjamesjacoby3 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

Confirmed Ryan's code and I had the same thoughts as Sergey.

Closing as worksforme.

comment:4 nacin3 years ago

  • Milestone Awaiting Review deleted

comment:5 F J Kaiser3 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Version changed from 3.0.1 to 3.0.4

I got the same issue as "ggus" (reporter):

// note: same result with & without reset
wp_reset_query();
query_posts( array( 'post_type' => 'whatever', 'post_status' => 'publish', 'order_by' => 'menu_order', 'order' => 'DESC' ) );

produces:

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'whatever' AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 10
Last edited 3 years ago by F J Kaiser (previous) (diff)

comment:6 F J Kaiser3 years ago

  • Cc 24-7@… added

comment:7 follow-up: SergeyBiryukov3 years ago

Shouldn't it be orderby instead of order_by?

comment:8 in reply to: ↑ 7 F J Kaiser3 years ago

Replying to SergeyBiryukov:

Shouldn't it be orderby instead of order_by?

Absolutely right. I wasn't aware, that there is a difference. Using for ex. get_posts needs order_by. Pretty unexpected. It took quite some hours without finding any answer. Thanks for your comment. Too bad that this is not consistend throught all functions...

Version 0, edited 3 years ago by F J Kaiser (next)

comment:9 F J Kaiser3 years ago

  • Resolution set to worksforme
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.