首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的自定义post类型将空值导入数据库自动

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

Stack Overflow用户
提问于 2018-01-16 19:44:37
回答 1查看 41关注 0票数 1

我做了一个自定义的帖子类型和自定义的meta box。当我按下“添加新帖子”时,它会自动将输入值导入到post_meta表中。我怎么才能修复它?我不想在发布帖子之前导入。我有这个代码。

代码语言:javascript
运行
复制
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' );

当我点击添加新的自定义帖子时,metas使用NULL将自动导入到mysql

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-16 19:54:49

在保存帖子元之前,您需要检查帖子状态。已使用get_post_status()。请参阅get_post_status()Post Status的Codex

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

代码语言:javascript
运行
复制
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 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48280493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档