edit.php
is the post/page list, the post creation/editing screen are 2 separate files: post-new.php
and post.php
.
so you can target using "load-$page"
hook, e.g.:
add_action( 'load-post.php', function() {
// add metabox or whatever you want when EDITING post
add_action( 'add_meta_boxes', 'myplugin_meta_box_editing' );
} );
add_action( 'load-post-new.php', function() {
// add metabox or whatever you want when creating NEW post
add_action( 'add_meta_boxes', 'myplugin_meta_box_new' );
} );
To get more specific information (e.g. the post type being cretaed/edited) as @KrzysiekDróżdż alredy said, you can use the current screen object, however I suggest you to use get_current_screen()
function for the porpose.
add_action( 'load-post.php', function() {
// add metabox or whatever you want when EDITING post
$screen = get_current_screen();
// this should give you an idea of the infromation you can get
// DO NOT USE IN PRODUCTION
echo '<pre>';
print_r( $screen );
die();
} );
When adding a metabox, as alterantive or in addiction to previous methods, the callback you use for to output the metabox (the 3rd param of add_metabox
function) receive the post id, so using get_post()
you can retrieve the post object being edited an look at the post_status
you can know if the post is just created or not:
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_add_meta_box() {
// do you need information about screen?
// this can be used, e.g. to use different metabox callback if
// editing or creating a new post
// $screen = get_current_screen();
add_meta_box(
'myplugin_sectionid', // metabox id
__( 'My Post Section Title', 'myplugin_textdomain' ), // metabox label
'myplugin_meta_box_callback', // metabox callback, see below
'post' // this metabox is for 'post' post type
);
}
function myplugin_meta_box_callback( $post_id ) {
$post = get_post( $post_id );
$status = $post->post_status;
if ( $status === 'new' || $status === 'auto-draft' ) {
// this is a new post
} else {
// editing an already saved post
}
}