WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im automatically creating pages with the below code, after that I create a custom menu and insert all the pages to this menu.

$create_pages = array('Home', 'About', 'Contact'); // there will be more items
$count = 1; 
foreach ($create_pages as $new_page) {
    $add_pages = array(
        'post_title' => $new_page,
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'page',
        'menu_order' => $count++ // incrementing the order value to overwrite default alphabetical ordering
    $page_id = wp_insert_post($add_pages);

So far I managed to actually overwrite the default alphabetical ordering and now on the Pages screen, all the pages are ordered the way I want.

$main_nav_id = wp_create_nav_menu('Main menu', array('slug' => 'main-nav'));
set_theme_mod('nav_menu_locations', array('main-nav' => $main_nav_id));

$pages = get_pages();
$count = 1; 
foreach($pages as $page) {
    $item = array(
        'menu-item-object-id' => $page->ID,
        'menu-item-object' => 'page',
        'menu-item-type' => 'post_type',
        'menu-item-status' => 'publish',
        'menu-item-position' => $count++ // im trying to use the same approach
    wp_update_nav_menu_item($main_nav_id, 0, $item);

So with this code, Im trying to use the same approach as I did before with the pages, but whatever I try, the menu items are still ordered in a weird way, I just want them to be ordered the same way as they are ordered on the Pages screen. How could I fix this?

share|improve this question
up vote 0 down vote accepted

You don't set sort_column to menu_order when you call get_pages, so it defaults to sorting on post_title.

$pages = get_pages( array( 'sort_column' => 'menu_order' ) );
share|improve this answer
I love you :D Thanks! – Richard Mišenčík Feb 16 '14 at 14:38

Your Answer


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.