Increment Page Order As Pages Are Created

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x