New page does not get a menu_order

The problem is that the order of the home page is not correct anymore. The new items are not in the same order as the menu, even though this is active:

'orderby'   => 'menu_order', 
'order'     => 'asc' 

In the database I can see that every new page, type=project, used to have a menu_order assigned but not anymore? The menu_order for every new page is 0.

How is this possible?

ADDED:

thank you for your help. Projects is a custom post type. In a custom page template I have the following code:

array(‘projects’),
‘orderby’ => ‘menu_order’,
‘order’ => ‘asc’,
‘post_status’ => ‘publish’ ));
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
The template is created by someone else. It has been explained to me by the site owner/editor that he changed the menu ordering and then the page displayed the projects in the same order. But now (since the new theme) this does not work anymore. The only thing I can find in the code is what you see above.
I added the page-attributes and now I can manually change the order. But this is still not what the client used to do..

1 Answer
1

Why this is happening

The default menu_order is 0, so unless you manually change this any new Page/Post inserted in to your DB will correctly have this menu_order set.

You can set the menu_order for any Page via Quick Edit as below (or indeed the Edit Page screen if you wish) –

Set Menu Order via Quick Edit

By default, Post and Custom Post Type posts don’t have the muen_order attribute, meaning that 0 will always be used, but this can be overridden (keep reading to see how).

I see from your question that mention type=project; I’m not sure if you are referring a Custom Post Type, or if type is a Custom Field.

Assuming “project” is a Custom Post Type

You can add the menu_order attribute any Custom Post Type very easily; simply ensure that you include page-attributes in the supports argument when declaring the Custom Post Type

'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'page-attributes'),

Assuming “project” is a Custom Field

If you are trying to display posts of the built-in type Post, meaning that project is a Custom Field, then you need to add the menu_order attribute to the Post type –

add_action('admin_init', 'add_post_page_attributes');
function add_post_page_attributes(){

    add_post_type_support( 'post', 'page-attributes' );

}

Changing the default “menu_order”

If you wish, you can use the following code to override the default menu_order and set it to whatever you desire.

I use this code myself because I have a unique requirement to order 1-100 posts in a non-standard order, followed by the remainder in alphabetical order. I use 100 as a default, but you can change this to whatever you want –

add_filter('wp_insert_post_data', 'set_default_menu_order', '99', 2);
function set_default_menu_order($data, $postarr){

    if($data['post_type'] === 'post') :                                                     // Ensure we're saving a desired Post Type...
        $data['menu_order'] = ($data['menu_order'] == '0') ? '100' : $data['menu_order'];   // ...Check to see if a 'menu_order' has been set; if so, keep it, if not, use the default
    endif;

    return $data;   // Return the new data

}

Leave a Comment