Increment Page Order As Pages Are Created

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

Leave a Comment