I have made a custom menu handler for the back end, sorting the via a drag drop based on CMS Page Order.

The problem now is that it seems that it's not possible to sort wp_nav_menu in any other way than the default one, can that be really be true?

  • 1
    Show us what you have tried already to make answers possible.
    – fuxia
    Jul 18, 2012 at 23:42
  • @toscho I am yet to find something to try out, the only code I've found is the one that reverses the array, not quite what I'm looking for. Maybe it's possible to do something in the walker, but it's too complex for me to understand (it's from the roots framework: pastebin.com/W5ja5dGJ)
    – INT
    Jul 18, 2012 at 23:56
  • @toscho I also found this, but not really sure if it applies to my problem: wordpress.stackexchange.com/a/52408/2830 Tried this to no avail: pastebin.com/5NHqC2S1
    – INT
    Jul 19, 2012 at 18:50

1 Answer 1


The nav_menu_items are already sorted by menu_order.

When you go into the Appearance -> Menus and arrange the order menu the menu_order of each menu item gets updated.

To change the orderby parameter in the query that displays the menu you can use pre_get_posts to alter it. Example:

add_action( 'pre_get_posts', 'wpse_sort_nav' );
function wpse_sort_nav( $query ) {
        if ( $query->is_main_query() && get_query_var( 'post_type' ) == 'nav_menu_item' )
            $query->set( 'orderby', 'ID' );

ID and menu_order are your only real options for sorting using the default post table. There is no title and the post_name is the same as the ID.

You could add additional parameters to the pre_get_posts filter using the meta data stored with each nav_menu_item.

Nav Menu metadata:

_menu_item_classes _menu_item_xfn


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.