Show box only on edit post

I’ve made a video custom post type and on the edit page I’ve placed a meta box where I display a shortcode associated to the post, to make it easier for the user.

However, I wish the shortcode and/or the box to be visible on the edit post page only and not on the new post page, where it’s not yet relevant – what am I looking for here? I have this nagging feeling I’ve done this/something similar before but just can’t think of what to search for.

Read More

ETA:
This is what I did with the help I got below, might be useful to someone else too:

add_action( 'add_meta_boxes', 'my_video_add_custom_box' );
function my_video_add_custom_box() {
    if (get_post_status( $post->ID ) != 'auto-draft') {
        add_meta_box(
            'my_video_shortcode_custom_box',
            __( 'Shortcode', 'my_video' ),
            'my_video_shortcode_custom_box',
            'my_video', 'side', 'default'
        );
    }
}

Works like a charm, thanks!

Related posts

2 comments

  1. Check the post status using get_post_status( $ID ) and then display the html code according to the status. ( i.e ) You could simply use if else condition in your php template file to display different content based on the post status. I think new posts have auto-draft status. get_post_status
    codex should be helpful.

  2. Your solution, incorrectly posted as an edit to the question, give me an “undefined variable” Notice for $post. It works because the chain of function calls triggered by get_post_status ultimately, if all else fails, assumes the global variable $post.

    A better solution would be to move the conditional out of registration function and into the callback, which is passed the $post variable by the WordPress core.

    add_action( 'add_meta_boxes', 'my_video_add_custom_box' );
    function my_video_add_custom_box() {
      add_meta_box(
          'my_video_shortcode_custom_box',
          __( 'Shortcode', 'my_video'),
          'my_video_shortcode_custom_box',
          'my_video', 
          'side', 
          'default'
      );
    }
    
    function my_video_shortcode_custom_box($post) {
      if (get_post_status( $post->ID ) != 'auto-draft') {
        // your box code
      }
    }
    

Comments are closed.