I have a custom post meta box that I want to put in the right sidebar. When I set the priority to high
it appears in the number 1 spot in sidebar above everything. If I set to core
it appear at the bottom of right sidebar.
My desired position is in the number 2 spot in the right sidebar. Right below the publish metabox.
Is this possibble?
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', 'xyz@example.com' );
$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 );