1

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!

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.

  • Thanks, I never would have thought to look for that. Will add my finished code to my question above. – Linda H Nov 19 '13 at 14:04
0

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
  }
}
  • I'm the first to admit, I still find it confusing to know when the $post variable is sent along or not, but I guess the first two rows should have looked like this instead: add_action( 'add_meta_boxes', 'my_video_add_custom_box', 10, 2 ); function my_video_add_custom_box($post_type, $post) { to make sure the $post is sent along. I could move the conditional but then I'd be left with an empty box and not without a box which was what I was after. – Linda H Nov 19 '13 at 17:11
  • Btw, I wasn't sure where else to post the code as it's not a new answer or full code but just a little snippet to show what I did. And it worked for me, whether it should have or not. – Linda H Nov 19 '13 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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