WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#21618 closed defect (bug) (fixed)

WP_Query should accept menu_order as a query arg

Reported by: wonderboymusic Owned by: ryan
Milestone: 3.5 Priority: normal
Severity: normal Version: 2.0
Component: Query Keywords: has-patch commit
Focuses: Cc:

Description

With custom post types, you can use the Posts table for basically anything. menu_order can be especially useful for holding foreign ids (after you add an index to it). The problem is that menu_order can't be passed to WP_Query:

add_filter( 'query', function ( $sql ) { error_log( $sql ); return $sql; } );
$stuff = new WP_Query( array( 'menu_order' => 5 ) );
exit();

// SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish')  ORDER BY wp_posts.post_date DESC LIMIT 0, 10

My patch fixes that, the result is now:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.menu_order = 5 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish')  ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Attachments (1)

menu-order-arg.diff (875 bytes) - added by wonderboymusic 3 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 @nacin3 years ago

Adding it to the public query vars list means it can be accessible via the URL, so ?menu_order=1. That does not seem to be desirable to expose on the frontend.

Adding it to the private query vars list means it can be passed to wp(). While not an issue of disclosure, I've only ever added things to that list that we needed in wp_edit_posts_query() or wp_edit_attachments_query().

Adding it to WP_Query, as the other part of the patch does, seems sufficient.

comment:2 @wonderboymusic3 years ago

patch updated like whoa

comment:3 @Mamaduka3 years ago

  • Cc georgemamadashvili@… added

comment:4 @nacin3 years ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 3.5

comment:5 @ryan3 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In [21748]:

Allow querying by menu_order. Props wonderboymusic. fixes #21618

comment:6 @azaozz3 years ago

In [21756]:

Add menu_order to fill_query_vars(), see #21618

Note: See TracTickets for help on using tickets.