When I create a new post of a Custom Post Type, my debug.log
gets filled with PHP Notice: Unidentified index:
warnings for each of the meta fields.
This question has been asked and answered before here and I implemented the solution there but each time I create a new post of this type, I am still seeing the errors in debug.log
.
The code for the save method is as follows:
/** Listener for saving post */
public function save() {
$post_type_name = $this->post_type_name;
add_action( 'save_post',
function () use ( $post_type_name ) {
// Do not autosave meta box data
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Abort if the nonce field is not set
if ( ! isset( $_POST['my-nonce'] ) ||
! wp_verify_nonce( $_POST['my-nonce'], MY__PLUGIN_FILE ) ) {
return;
}
global $post;
if ( isset( $_POST ) && isset( $post->ID ) && get_post_type( $post->ID ) == $post_type_name ) {
global $custom_fields;
// Loop through all meta boxes
foreach ( $custom_fields as $title => $fields ) {
// Loop through all fields in meta box
foreach ( $fields as $label => $type ) {
$field_name = self::uglify( $title ) . '_' . self::uglify( $label );
// Prevent PHP Warnings for undefined index
if ( isset( $_POST['fitcase'][ $field_name ] ) ) {
$metadata = $_POST['fitcase'][ $field_name ];
} else {
$metadata = null;
}
update_post_meta( $post->ID, $field_name, $metadata );
}
}
}
}
);
}
echo $_POST['my-nonce']
? I imagine it is NULL, but have you tried$_REQUEST
instead of$_POST
?add_meta_box()
call in a different method. When I do aprint_r()
of$_POST
I get an empty array and for$_REQUEST
I just get an array with thepost_type
key set.isset($meta[ $field_id_name ])
and if it fails it sets$meta[ $field_id_name ][0] = null;
and that cleared it.