In the Admin Panel, is there a way to hook into the page creation and increment Page Order number of each page or to be equal to the number of pages. Right now the default is 0
and when I make a new page i would much rather have it at the bottom of my list than the top.
The following is a possible solution, but needs some testing to confirm it is viable. It runs only if menu_order == 0
and if it's not an auto-draft or revision being saved
add_action( 'save_post', 'incremental_save_wpse_113767', 10, 2 );
function incremental_save_wpse_113767( $post_id, $post_object )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
# Block auto-drafts and revisions
# http://codex.wordpress.org/Post_Status_Transitions
if( in_array( $post_object->post_status, array( 'auto-draft', 'inherit' ) ) )
return;
# Menu order already set, do nothing
if( 0 != $post_object->menu_order )
return;
$menu_order = get_option( 'my_menu_order' );
if( !$menu_order)
$menu_order = 5; // <-- Adjust to the desired initial value
$post_object->menu_order = $menu_order;
remove_action( 'save_post', 'incremental_save_wpse_113767' );
wp_update_post( $post_object );
add_action( 'save_post', 'incremental_save_wpse_113767', 10, 2 );
$menu_order += 5;
update_option( 'my_menu_order', $menu_order );
}
-
I tried this but it unfortunately doesn't increment it. If i change
$menu_order = 5
to ->$menu_order = 1
and change$menu_order += 5
to$menu_order += 1
it just sets it to 1. – Howdy_McGee♦ Mar 5 '14 at 20:37
I am fairly sure that this will do what you want without having to mess with the "Page Order" value.
add_action(
'pre_get_posts',
function($qry) {
if (is_admin() && 'page' == $qry->get('post_type')) {
$qry->set('orderby','ID');
$qry->set('order','ASC');
}
},
1000
);
Post/Page IDs are always sequential so if you order by the ID from smallest to largest you should get the newer content at the bottom.
-
1I also found a similar solution, the problem is it messes up my sortable columns, as in once this code is implemented, my sortable columns no longer sort. – Howdy_McGee♦ Sep 11 '13 at 15:31
I've taken brasofilo code and improved it to retrieve the last post of the same post type to increment the menu_order
. This way you don't need to store the last menu_order
value in options. Code just below:
function wp1234_save_post_menu_order_increment( $post_id, $post ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ||
defined( 'DOING_AJAX' ) && DOING_AJAX ||
in_array( $post->post_status, array( 'auto-draft', 'inherit' ) ) ||
$post->menu_order != 0 ) {
return;
}
// Set a default menu order
$menu_order = 1;
// Get the previous post of the same post type
$prev_post = get_posts(array(
'post_type' => $post->post_type,
'author' => get_current_user_id(),
'posts_per_page' => 1,
'orderby' => array(
'menu_order' => 'desc'
)
));
// Check if there are any previous posts of the same post type
if ( count( $prev_post ) > 0 ) {
// Increment menu order
$menu_order = $prev_post[0]->menu_order + 1;
}
// Update the post menu order
wp_update_post(array(
'ID' => $post->ID,
'menu_order' => $menu_order
));
}
add_action( 'save_post', 'wp1234_save_post_menu_order_increment', 10, 2 );