I have a custom-post-type named wr_events. I have quite a lot of meta-boxes in this post-type. The post-type and the meta-boxes itself work just fine.

Only thing: No matter at what post-type I create a "New post" I get quite a lot of php errors and notices …

Notice: Undefined index: _wr_event_speaker in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 246

Notice: Undefined index: _wr_event_date in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 248

Notice: Trying to get property of non-object in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 249

Notice: Trying to get property of non-object in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 250

Notice: Undefined index: _wr_event_time in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 250

Notice: Trying to get property of non-object in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 251

Notice: Undefined index: _wr_event_review in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 251

So even though this meta-boxes are just assigned to my "events" post-type the errors above also come up when I create a normal new post.

So again, if I click on "Add New" this messages come up. They also appear if I goto "Add New" in my custom-post-type.

Weird is, that the errors don't come up if I edit a post or a custom-post.

Any ideas what could cause that?


Well I use a class for the custom metaboxes because my requirements are quite unusual. https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress

 * Initialize the post-meta class

add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );
function be_initialize_cmb_meta_boxes() {
    if ( !class_exists( 'cmb_Meta_Box' ) ) {
        require_once( 'lib/metabox/init.php' );

add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );

function cmb_sample_metaboxes( array $meta_boxes ) {

    $prefix = '_wr_';

    $meta_boxes[] = array(
        'id'         => 'event_review',
        'title'      => 'Review',
        'pages'      => array( 'wr_event'),
        'context'    => 'normal',
        'priority' => 'low',
        'fields'     => array(
                'name'    => 'Review',
                'id'      => $prefix . 'event_review',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),

    return $meta_boxes;

So, again: This works like a charm and the notices actually shouldn't bother me because everything is working fine. However I was just wondering why this notices appear on every "Add New" page.


The update_post_meta() functions throw the error.

add_action('save_post', 'save_details');

function save_details(){
    global $post;
    update_post_meta($post->ID, "event_review", $_POST["_wr_event_review"]);
  • 1
    could you post the code where you register the offending metabox? Commented Jun 6, 2012 at 15:55
  • Where is the code used to create/generate the custom meta boxes? Commented Jun 6, 2012 at 15:55
  • could you post code from file /Users/my/htdocs/wr/wp-content/themes/wr/functions.php at lines 235~260? Commented Jun 6, 2012 at 16:17

2 Answers 2


Start by reading the notices--they tell you very explicitly what the problem is.

Let's start with the first one:

Notice: Undefined index: _wr_event_speaker in /Users/my/htdocs/wr/wp-content/themes/wr/functions.php on line 246

First, this is just a "notice". Meaning that no error has actually occurred. PHP is just telling you that something isn't quite right, but that it skipped over the problem and continued execution of the rest of the script.

Next, the notice itself is "Undefined index: _wr_event_speaker". "Undefined index" means that you're trying to access an array key that doesn't actually exist (the key being "_wr_event_speaker"). In the case of meta boxes, you're probably doing something like:

<input type="text" name="something" value="<?php echo $my_array['_wr_event_speaker'] ?>" />

But since you're creating a NEW post, there is no value set for _wr_event_speaker yet, so you're trying to echo something that doesn't exist, and the notice is triggered. This explains why the notices only happen on new posts--old posts DO have a value set for _wr_event_speaker, so there's no problem.

Now, look at the very end of the notice. It tells you that the notice was triggered on line 246 in the file "/Users/my/htdocs/wr/wp-content/themes/wr/functions.php". This is where you'll see the offending code (which probably looks something like my example above).

You can avoid these kinds of warnings in a lot of different ways. All of them involve checking if the array key is actually set before you try to access it. One way (if you want to do it inline) is to use "Ternary operators" in your echo statement, like so:

<input type="text" name="something" value="<?php echo ( isset( $my_array['_wr_event_speaker'] ) ? $my_array['_wr_event_speaker'] : '' ); ?>" />

This says to php: "if $my_array['_wr_event_speaker'] is set, print its value, otherwise print an empty string". And it will stop the notice from being tripped.

It's a best practice to always check whether or not an array key or object property exists before trying to print it.


Here are the modifications I'd suggest based on the code snip you posted. It look like a lot, but I've added some comments to (I hope) make it more clear. The gist of it is, besides using an "isset" (the original crux of your issue), you should always add the below boilerplate when hooking any function to the save_post action. There is some weirdness that needs to be accounted for to avoid future issues.

add_action('save_post', 'save_details');

function save_details($post_id){

    // Check for autosaves and quickedits. This is really important.
    // These types of saves DON'T pass in any custom meta box values, so for autosaves
    // and quickedits, just bail right away, or our post meta values will be
    // overwritten with blank ones!
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || (defined('DOING_AJAX') && DOING_AJAX) ) 

    // The "save_post" action runs whenever any item of any type is saved, so you have to figure out what kind of item this is, and handle it accordingly.
    // Otherwise, your DB will fill up with blank values for this post meta, associated with all post types.
    $post_type = isset($_POST['post_type']) ? $_POST['post_type'] : '';

    switch($post_type) {

        // If this is the "post" post type
        case "post":

            // Update the _wr_event_review value
            $wr_event_review = isset($_POST['_wr_event_review']) ? wp_filter_post_kses($_POST['_wr_event_review']) : '';
            update_post_meta($post->ID, 'event_review', $wr_event_review);





Note: you should really be sanitizing the input at this point. Looks like this is a textarea? In which case, you at least want to pass $_POST["_wr_event_review"] through wp_filter_post_kses() before saving.

  • Thank you, the isset() however is quite tough here because the inputs are handled entirely by the class, right? I do check the outputs already with isset in my templates for my custom-post-type to prevent this errors. However in the backend I have no idea how to do so since the metaboxes are created by the class or wordpress itself. Commented Jun 6, 2012 at 17:32
  • @mathiregister It's tough to tell without seeing the relevant code. According to the warnings, the issue is in your theme's functions file (not in the class). It's possible that these lines reference the class, but I can't tell without seeing them :) If you could post the relevant lines from your functions file (lines 246-252), it might help.
    – MathSmath
    Commented Jun 6, 2012 at 17:39
  • I found the relevant lines … the update_post_meta() functions where I save the details of the meta-boxes are throwing the notices … update_post_meta($post->ID, "event_speaker", $_POST["_wr_event_speaker"]); Should I use isset() here as well? Commented Jun 6, 2012 at 18:24
  • @mathiregister It'd be much easier to help if you'd post the relevant lines form your functions file. Otherwise, I don't know what you're actually trying to do there...like, why do you update the post meta every time the site loads? Seems weird for anything execept a "view counter". If I could see the whole chunk of code, and know what you were trying to accomplish, I could offer better advice.
    – MathSmath
    Commented Jun 6, 2012 at 18:26
  • @mathiregister RE your comment "I found the relevant lines"--yes, you should always use isset or something similar when attempting to access a particular array key, unless you know for sure it's been set already.
    – MathSmath
    Commented Jun 6, 2012 at 18:28

The 'include if ( class_exists )' needs to be called after you define your meta boxes or you can just define an empty array right above the include block.

$meta_boxes = array();


You can also get support directly from the developers and other users by creating an issue. The closed issues are also there to browse.

Update based on your update:

Your update function needs to wrap the variable in isset(); to prevent the notices.

You also need a nonce check, conditional check to make sure your on the right post_type and a delete_post_meta is helpful when you clear out a field.

add_action('save_post', 'save_details');
function save_details( $post_id, $post ){
    if ( !wp_verify_nonce( $_POST [ 'you_are_using_nonces_right' ], plugin_basename( __FILE__ ) )) 
         return $post->ID; 
    if ( ( 'revision' == $post->post_type ) || ( 'wr_event' !== $post->post_type ) ) 
         return $post->ID ; 
    $value = isset( $_POST['_wr_event_review'] ) ? $_POST[_wr_event_review] : null;
    update_post_meta($post->ID, '_event_review', $value );
    if (! $value ) delete_post_meta( $post ->ID, '_event_review' ); 

  • I do actually include the class after I define my metaboxes, just posted them the wrong way above in the question. The $meta_boxes = array(); doesn't seem to to stop the notices. The issue on github however is actually about something else - not the same problem it seems. Commented Jun 6, 2012 at 17:35
  • Got it. I didn't realize you were using your own save function. See updated answer.
    – Chris_O
    Commented Jun 6, 2012 at 18:36

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.