acf/pre_save_post

Last updated Apr 4, 2024

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

  • Added in v4.0.0

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

?>
Supercharge Your Website With Premium Features Using ACF PRO

Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.

Explore Features View Pricing

PRO Features
ACF Blocks
Options Pages
PRO Fields
Repeater
Flexible Content
Gallery
Clone

Related