Wordpress acf when save post

2019-08-08 22:52发布

When I save the post It create one post with correct value and an other with no title and no value!

add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );

function tsm_do_pre_save_post( $post_id ) {


        // Create a new post
        $post = array(
            'post_type'     => 'itemfounds', // Your post type ( post, page, custom post type )
            'post_status'   => 'draft', // (publish, draft, private, etc.)
            'post_title'    => 'Δωρεά σε είδος για το "'.get_the_title(wp_strip_all_tags( $_POST['acf']['field_5696694332974'] )).'"' , // Post Title ACF field key 
        );

    // insert the post
    $post_id = wp_insert_post( $post );

    session_start();

    $_SESSION['item_pid'] = $post_id;

    // Save the fields to the post
   // do_action( 'acf/save_post' , $post_id );


    return $post_id;

}

1条回答
唯我独甜
2楼-- · 2019-08-08 23:38

You can use acf by this way:-

$post_id (array) the ID of the post being saved

BEFORE

<?php

function my_acf_save_post( $post_id ) {

    // bail early if no ACF data
    if( empty($_POST['acf']) ) {

        return;

    }


    // array of field values
    $fields = $_POST['acf'];


    // specific field value
    $field = $_POST['acf']['field_abc123'];

}

// run before ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 1);

?>

AFTER

<?php

function my_acf_save_post( $post_id ) {

    // get new value
    $value = get_field('my_field');


    // do something

}

// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);

?>

Hope this will help you :)

查看更多
登录 后发表回答