我的自定义 post 类型将空值自动导入数据库

My custom post type importing empty value to database auto

我用自定义元框制作了自定义 post 类型。当我按下 "add new post" 时,它会自动将输入值导入 post_meta table。我该如何解决?我不想在发布 post 之前导入。我有这个代码。

function wpdocs_save_meta_box_mac($post_ID = 0) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return $post_ID;
    }
    $post_ID = (int) $post_ID;
    $post_type = get_post_type( $post_ID );
    if ( "mactahminleri" == $post_type ) {

        // Sanitize user input.
        $_mac_baslik = sanitize_text_field( $_POST['_mac_baslik'] );
        $_mac_tarih = sanitize_text_field( $_POST['_mac_tarih'] );
        $_mac_saat = sanitize_text_field( $_POST['_mac_saat'] );
        $_mac_kod = ( $_POST['_mac_kod'] );
        $_mac_tahmin = sanitize_text_field( $_POST['_mac_tahmin'] );
        $_mac_sonuc_iy = ( $_POST['_mac_sonuc_iy'] );
        $_mac_sonuc_ms = ( $_POST['_mac_sonuc_ms'] );
        $_mac_oran = ( $_POST['_mac_oran'] );
        $_mac_afflink = ( $_POST['_mac_afflink'] );
        $_mac_durum = ( $_POST['_mac_durum'] );

        // Update the meta field in the database.
        update_post_meta( $post_ID, '_mac_baslik', $_mac_baslik );
        update_post_meta( $post_ID, '_mac_tarih', $_mac_tarih );
        update_post_meta( $post_ID, '_mac_saat', $_mac_saat );
        update_post_meta( $post_ID, '_mac_kod', $_mac_kod );
        update_post_meta( $post_ID, '_mac_tahmin', $_mac_tahmin );
        update_post_meta( $post_ID, '_mac_sonuc_iy', $_mac_sonuc_iy );
        update_post_meta( $post_ID, '_mac_sonuc_ms', $_mac_sonuc_ms );
        update_post_meta( $post_ID, '_mac_oran', $_mac_oran );
        update_post_meta( $post_ID, '_mac_afflink', $_mac_afflink );
        update_post_meta( $post_ID, '_mac_durum', $_mac_durum );
    }
    return $post_ID;

}

add_action( 'save_post', 'wpdocs_save_meta_box_mac' );

当我点击添加新自定义 post 时,metas 自动导入到 mysql NULL

您需要在保存 post 元数据之前检查 post 状态。使用 get_post_status()。请参阅 get_post_status() and Post Status

的法典

在保存元值之前添加此条件

function wpdocs_save_meta_box_mac($post_ID = 0) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return $post_ID;
    }
    $post_ID = (int) $post_ID;
    $post_type = get_post_type( $post_ID );
    if ( "mactahminleri" == $post_type ) {
        if ( get_post_status ( $post_ID ) == 'publish' ) {
            // Sanitize user input.
            $_mac_baslik = sanitize_text_field( $_POST['_mac_baslik'] );
            $_mac_tarih = sanitize_text_field( $_POST['_mac_tarih'] );
            $_mac_saat = sanitize_text_field( $_POST['_mac_saat'] );
            $_mac_kod = ( $_POST['_mac_kod'] );
            $_mac_tahmin = sanitize_text_field( $_POST['_mac_tahmin'] );
            $_mac_sonuc_iy = ( $_POST['_mac_sonuc_iy'] );
            $_mac_sonuc_ms = ( $_POST['_mac_sonuc_ms'] );
            $_mac_oran = ( $_POST['_mac_oran'] );
            $_mac_afflink = ( $_POST['_mac_afflink'] );
            $_mac_durum = ( $_POST['_mac_durum'] );

            // Update the meta field in the database.
            update_post_meta( $post_ID, '_mac_baslik', $_mac_baslik );
            update_post_meta( $post_ID, '_mac_tarih', $_mac_tarih );
            update_post_meta( $post_ID, '_mac_saat', $_mac_saat );
            update_post_meta( $post_ID, '_mac_kod', $_mac_kod );
            update_post_meta( $post_ID, '_mac_tahmin', $_mac_tahmin );
            update_post_meta( $post_ID, '_mac_sonuc_iy', $_mac_sonuc_iy );
            update_post_meta( $post_ID, '_mac_sonuc_ms', $_mac_sonuc_ms );
            update_post_meta( $post_ID, '_mac_oran', $_mac_oran );
            update_post_meta( $post_ID, '_mac_afflink', $_mac_afflink );
            update_post_meta( $post_ID, '_mac_durum', $_mac_durum );
        }
    }
    return $post_ID;

}

add_action( 'save_post', 'wpdocs_save_meta_box_mac',99,1 );