Automatically set page order on create page

Is there a way to automatically set page order to the last order number upon creating new pages instead of having it set to 0?

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

You could use ajax and the admin_footer-post-new.php hook. The sql would vary depending on whether you want the highest or most recently published order number. The following returns the highest published order number + 1:

function wpse155926_set_menu_order() {
    $ret = array();

    if ( check_ajax_referer( 'wpse155926_set_menu_order_post', 'nonce', false /*die*/ ) ) {
        global $wpdb;
        //last published
        //$sql = $wpdb->prepare( 'SELECT menu_order FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_status = %s ORDER BY post_date DESC LIMIT 1', 'page', 'publish' );
        //highest published
        $sql = $wpdb->prepare( 'SELECT menu_order FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_status = %s ORDER BY menu_order DESC LIMIT 1', 'page', 'publish' );
        if ( ( $result = $wpdb->get_var( $sql ) ) !== false ) {
            $ret['menu_order'] = $result + 1;
        }
    }

    header('Content-type: application/json');
    die( json_encode( $ret ) );
}
add_action( 'wp_ajax_wpse155926_set_menu_order', 'wpse155926_set_menu_order' );

function wpse155926_admin_footer_post_new_php() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
    (function ($) {
        $.post( ajaxurl, {
                action: 'wpse155926_set_menu_order',
                nonce: <?php echo json_encode( wp_create_nonce( 'wpse155926_set_menu_order_post' ) ); ?>
            }, function(response) {
                if (response && response.menu_order) {
                    $('#pageparentdiv input[name="menu_order"]').val(response.menu_order);
                }
            }, 'json'
        );
    })(jQuery);
});
</script>
<?php
}
add_action( 'admin_footer-post-new.php', 'wpse155926_admin_footer_post_new_php' );

Method 2

You could hook into the publish_page transitional status action and use a simple SQL query (via $wpdb) to determine the highest current value of menu_order in the {prefix}_posts table and set the menu_order of the new page accordingly. Example code:

add_action( 'publish_page', 'wpse155926_set_to_last_page', 10, 2 );
function wpse155926_set_to_last_page( $ID, $post ) {
  if ( $post->menu_order === 0 ) {
    global $wpdb;
    $query = "SELECT MAX(menu_order) FROM $wpdb->posts WHERE post_type LIKE 'page' AND post_status LIKE 'publish';";
    $max_menu_order = $wpdb->get_var( $query );
    $post->menu_order = ++$max_menu_order;
    remove_action('publish_page', 'wpse155926_set_to_last_page');
    wp_update_post( $post );
    add_action('publish_page', 'wpse155926_set_to_last_page');
  }
}


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