保存帖子时的Wordpress acf

时间:2016-01-18 11:00:40

标签: php wordpress function post advanced-custom-fields

当我保存帖子它创建一个具有正确值的帖子和另一个没有标题且没有值的帖子!

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 个答案:

答案 0 :(得分:1)

您可以通过这种方式使用acf: -

$post_id(数组)要保存的帖子的ID

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

?>

希望这会对你有所帮助:)。