acf/pre_save_post


目录

Description

This filter allows you to modify the $post_id before any custom field data is saved from the front end form (acf_form()). This filter is used in the tutorial Using acf_form to create a new post.

This filter is not required to create a new post due to the new_post argument added to acf_form() in v5.0.0

Changelog

Parameters

Name Type Description
post_id int The post ID to which all the $_POST data (values) will be saved to

Usage

This code could be used in the functions.php file, or on the page template which features the front end form (above the acf_form_head function)

functions.php

<?php

function my_pre_save_post( $post_id ) {

    // check if this is to be a new post
    if( $post_id != 'new' ) {

        return $post_id;

    }

    // Create a new post
    $post = array(
        'post_status'  => 'draft' ,
        'post_title'  => 'A title, maybe a $_POST variable' ,
        'post_type'  => 'post' ,
    );  

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

    // return the new ID
    return $post_id;

}

add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

?>

版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC