1

I have made a custom post type with custom meta box. when i press "add new post" its auto importing input values to post_meta table. how can i fix it? i don't want to import before publish post. I have this code.

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' );

when I click to add new custom post the metas importing auto to mysql with NULL

Narek Zakarian
  • 215
  • 1
  • 10

1 Answers1

1

You need to check the post status before save the post meta. used get_post_status(). See Codex for get_post_status() and Post Status

Add this condition before save meta values

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 );
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38