Still if its something like client requirement then below is somewhat hack sort of solution. Add this code to your add_meta_box()
function. For the sake of understanding, below I have provided complete function but you will need to use selective code 🙂 Do let me know how it goes.
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'testdiv',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen,
'side'
);
}
/* Get the user details to find user id for whom this order should be shown. Ideally, I believe it will be admin user. Make sure you change the email id*/
$user = get_user_by( 'email', '[email protected]' );
$order = get_user_option("meta-box-order_post", $user->ID);
$current_order = array();
$current_order = explode(",",$order['side']);
for($i=0 ; $i <= count ($current_order) ; $i++){
$temp = $current_order[$i];
if ( $current_order[$i] == 'testdiv' && $i != 1) {
$current_order[$i] = $current_order[1];
$current_order[1] = $temp;
}
}
$order['side'] = implode(",",$current_order);
update_user_option($user->ID, "meta-box-order_page", $order, true);
update_user_option($user->ID, "meta-box-order_post", $order, true);
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box', 2 );