2

This question came up when creating a custom meta box for the edit.php page.

Is there a conditional statement to target posts that are already exist vs posts that are not created yet?

The idea:

If editing post (post exists) {
 // Display custom input field
} else if adding post (post does not exist)
 // Hide custom input field
}

This snippet would be used in a custom meta box.

I wasn't too sure what to search for, because it seemed to broad...

Would the simplest (and efficient) solution be to check if a post title exist? I haven't tried it yet - but it seems that it would work.

2 Answers 2

3

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
   }
}
2
  • $status snippet worked wonderfully. I placed it within my metabox. No need to created additional action and function codes. Thanks!
    – Roc
    Apr 23, 2014 at 3:43
  • @Roc yes, it works :) the "load-$page" hook is needed when you want performa an action early, e.g. add some css to page.
    – gmazzap
    Apr 23, 2014 at 9:15
1

Something like this should work:

global $current_screen;

if ( $current_screen->action == 'add' ) {
    // you're adding new post
} else {
    // you're editing...
}

If you want to do this only with one post type, you can check id field like this:

if ( $current_screen->action == 'page' ) ... // or post, etc.

You need to be careful. You shouldn't chceck $current_screen before it is set. Any hook after current_screen should be OK (so wp hook should work like a charm, but in init hook this object will be NULL).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.