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.
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!
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.
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 byget_post_status
ultimately, if all else fails, assumes theglobal
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.