Batch update menu_order attribute alphabetically

Is there a way to batch update the page order attribute of all children of a specific parent, each time a new child is added?

More simply, is there a way to loop through pages and change the sort attribute so they are alphabetical?

For instance….

  • Page Title A – sort order 0
  • Page Title C – sort order 0
  • Page Title B – sort order 0

Would become…

  • Page Title A – sort order 1
  • Page Title C – sort order 3
  • Page Title B – sort order 2

And would ultimately be displayed in the admin as…

  • Page Title A – sort order 1
  • Page Title B – sort order 2
  • Page Title C – sort order 3

Solutions Collecting From Web of "Batch update menu_order attribute alphabetically"

Hopefully this helps someone else

Dont forget to change “your_post_parent_id_here” with the parent id of your choosing.

    global $wpdb;
    $wpdb->query( 'SELECT @i:=-1' );
    $result = $wpdb->query(
        "
        UPDATE wp_posts SET menu_order = ( @i:= @i+1 ) WHERE post_parent = 'your_post_parent_id_here' ORDER BY post_title;
        "
    );