0

I have done this before but it doesn't seem to be working in this case. I am trying to get the category ID from the supplied slug and then add a metabox to the page if the page id matches the category id. My site is throwing me two errors in the admin area

Undefined index: post and undefined index post_id

    add_action('admin_init', 'add_meta_boxes', 1);
 function add_meta_boxes() {

    global $post;
     $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
     $cat = get_category_by_slug('audio');
     $id = $cat->term_id;
     if ($post_id == $id)
   {
     add_meta_box( 'repeatable-fields', 'Audio Playlist', 'repeatable_meta_box_display', 'post', 'normal', 'high');
   }
 }
1

I got it to work by doing it this way

 if (is_admin() ){
 $post_id = isset($_GET['post']) ? $_GET['post'] : isset($_POST['post_ID']) ;
 if( $post_id && in_category('audio', $post_id) ){

    add_action('admin_init', 'add_meta_boxes', 1);
  }
}

The only problem with this method is that it won't display the metabox until after you have published the post. The $post_id variable shows bool(false) until you publish.

so the big problem is that when you go to make a new post, there is no category ID to get. So using the link posted to Toscho's post, you can add category information and use $_GET to test for that information. Looks like it gets populated when you tick the category for that post.

0

I'd recommend using an action to place your meta box:

function my_add_meta_box() {
    global $post;
    if ( $post && is_a( $post, 'WP_Post' ) ) {
        $cat = get_category_by_slug('audio');
        if ( !is_wp_error($cat) ) {
            if ( $post->ID == $cat->term_id ) {
               add_meta_box( 'repeatable-fields', 'Audio Playlist', 'repeatable_meta_box_display', 'post', 'normal', 'high');
            } else {
               # for debug only
               wp_die("Post ID {$post->ID} and Cat ID {$cat->term_id}");
            }
        } else {
           # For debug only
           wp_die('Cat not found by slug!');
        }
    } else {
        # For debug only
        wp_die('Post not found in global space!');
    }
}
add_action('admin_init', 'my_add_meta_box');

Edit - removed check from box display ... so box is gone and not just empty.

function repeatable_meta_box_display( $post ) {
   # Draw out your meta box fields

}

This will change the logic, but the effect should be the same and you should stop getting undefined index errors.

You are getting the errors, because not all admin pages have a post id. Using an action will make the code only work when needed.

If it works otherwise and you just want to get rid of the errors, try the following in place of your checking code:

global $post;
if ( $post && is_a( $post, 'WP_Post' ) ) {
   $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
   $cat = get_category_by_slug('audio');
   $id = $cat->term_id;
   if ($post_id == $id) {
      # Add meta box call here
   }
}

That should make sure that post is what you are looking for.

You might have to add a second action later in the wordpress load to check post (maybe it is not declared at admin_init) and then remove the meta box if not wanted.

# Revised version without debug info

# Add the meta box
function my_add_meta_box() {
   add_meta_box( 'repeatable-fields', 'Audio Playlist', 'repeatable_meta_box_display', 'post', 'normal', 'high');
}
add_action('admin_init', 'my_add_meta_box');


# Remove the meta box if not needed once post is available
function maybe_remove_my_boxes() {
   $remove = true;
   global $post;
   if ( $post && is_a( $post, 'WP_Post' ) ) {
      $cat = get_category_by_slug('audio');
      if ( !is_wp_error($cat) ) {
          if ( $post->ID == $cat->term_id ) {
             $remove = false;
          }
      }
   }
   if ( $remove ) {
       remove_meta_box( 'repeatable-fields', 'post', 'normal' );
       # echo( 'Removed as not needed' );
   }
}
add_action('admin_head', 'maybe_remove_my_boxes');
  • there is an action. I will edit the code to show that. – Jamie Jan 15 '15 at 2:33
  • Added how to ensure post is available before trying to use it. – Privateer Jan 15 '15 at 2:55
  • no, unfortunately it isn't working. It's like the post variable is empty. The errors are gone but the metabox does not show in any category – Jamie Jan 15 '15 at 3:13
  • Try the edited version that has die statements in it. That should tell you where the problem lies specifically and maybe provide some insight as to where the problem is. – Privateer Jan 15 '15 at 3:14
  • says Post not found in global space – Jamie Jan 15 '15 at 3:25

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.