创建自定义帖子类型的新帖子时debug.log
充满了PHP Notice: Unidentified index:
每个元字段的警告。
这个问题以前已经问过并回答过了here 我在那里实现了解决方案,但每次我创建这种类型的新帖子时,我仍然会看到debug.log
.
save方法的代码如下:
/** 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 );
}
}
}
}
);
}