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) –
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
}