Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Show us what you have tried already to make answers possible. –  toscho Jul 18 '12 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 '12 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 '12 at 18:50
add comment

1 Answer 1

up vote 5 down vote accepted
+50

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_menu_item_parent
_menu_item_type
_menu_item_object_id
_menu_item_object
_menu_item_target
_menu_item_classes _menu_item_xfn
_menu_item_url

share|improve this answer
    
Thanks for your comprehensive answer. I realize now, that the approach I had to this issue the opposite of what I should be looking for. I have implented a custom menu, similar to the one that you find in Apperance -> Menus. What I want to do is send the order created in this menu to wp_nav_menu so that is the order seen in the front end. Sorry for this. I put up a new question if you have any insight (wordpress.stackexchange.com/q/59279/2830) Thanks! –  INT Jul 22 '12 at 15:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.