Post Action

The Post Action is a template to create or update a post on form submission.

Post Settings

Setting nameDescription
Action typeCreate or update a post
Action nameAlias name allowing to use targeted hook
TargetThe post to update
SourceThe post to retrieve fields from
Load valuesFill inputs with values
Post typeThe post type
Post statusThe post status
Post slugThe post slug
Post contentThe post content
Post authorThe post author
Post parentThe parent post
Post termsThe post terms
Save ACF fieldsChoose which ACF fields should be saved to this post
Load ACF fieldsChoose which ACF fields should have their values loaded

Validation

By default, there is no validation for this Action. You can use the acf/validate_value hook to validate each fields individually.

To validate the whole form, you can use the acfe/form/validation/post hook and throw errors with the acfe_add_validation_error() function. Usage example:

/*
 * Post Validation
 * 
 * @array   $form     Form settings
 * @string  $post_id  Post ID (where the form is displayed)
 * @string  $action   Action name
 */
 
action('acfe/form/validation/post',                $form, $post_id, $action);
action('acfe/form/validation/post/form=my-form',   $form, $post_id, $action);
action('acfe/form/validation/post/action=my-post', $form, $post_id, $action);
add_action('acfe/form/validation/post/form=my-form', 'my_form_validation', 10, 3);
function my_form_validation($form, $post_id, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
        
        // Add validation error
        acfe_add_validation_error('my_field', 'The value Company is not allowed');
        
    }
    
}

Prepare

The preparation phase is triggered after the form validation and before its submission. The acfe/form/prepare/post hook allows you to stop the Action early if necessary.

It is also the best place to add some logic or create new query vars that could be used within the Action UI. Returning false will stop the Action and go to the next one. Usage example:

/*
 * Post Prepare
 * 
 * @bool    $prepare  True/false
 * @array   $form     Form settings
 * @string  $post_id  Post ID (where the form is displayed)
 * @string  $action   Action name
 */
 
filter('acfe/form/prepare/post',                $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/post/form=my-form',   $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/post/action=my-post', $prepare, $form, $post_id, $action);
add_filter('acfe/form/prepare/post/form=my-form', 'my_form_prepare', 10, 4);
function my_form_prepare($prepare, $form, $post_id, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    // Create custom message
    $message = 'This is a custom message. Field value: ' . $my_field;
    
    // Retrive the message using {query_var:post_message} in the Post Action UI
    set_query_var('post_message', $message);
    
    // Do not create/update post if user isn't logged in
    if(!is_user_logged_in()){
        
        $prepare = false;
        
    }
    
    // return
    return $prepare;
    
}

Values Source

Alter the post ID where meta values are loaded from. Usage example:

/*
 * Post Values Source
 * 
 * @int     $post_id  Post ID Source
 * @array   $form     Form settings
 * @string  $action   Action name
 */

filter('acfe/form/load/post_id',                $post_id, $form, $action);
filter('acfe/form/load/post_id/form=my-form',   $post_id, $form, $action);
filter('acfe/form/load/post_id/action=my-post', $post_id, $form, $action);
add_filter('acfe/form/load/post_id/form=my-form', 'my_form_post_source', 10, 3);
function my_form_post_source($post_id, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Force to load values from the post ID 145
        $post_id = 145;
    
    }
    
    // return
    return $post_id;
    
}

Post Arguments

Alter the post arguments before database insert/update. Usage example:

/*
 * Post Arguments
 * 
 * @array   $args    Generated post arguments
 * @string  $type    Action type: 'insert_post' or 'update_post'
 * @array   $form    Form settings
 * @string  $action  Action name
 */

filter('acfe/form/submit/post_args',                $args, $type, $form, $action);
filter('acfe/form/submit/post_args/form=my-form',   $args, $type, $form, $action);
filter('acfe/form/submit/post_args/action=my-post', $args, $type, $form, $action);
add_filter('acfe/form/submit/post_args/form=my-form', 'my_form_post_args', 10, 4);
function my_form_post_args($args, $type, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Change post title
        $args['post_title'] = 'My title';
    
    }
    
    // Stop post creation/update
    // return false;
    
    // return
    return $args;
    
}

Submit

Trigger a custom action after the post was created/updated using the acfe/form/submit/post hook. Usage example:

/*
 * Post Submit
 * 
 * @int     $post_id  Created/Updated post ID
 * @string  $type     Action type: 'insert_post' or 'update_post'
 * @array   $args     Generated post arguments
 * @array   $form     Form settings
 * @string  $action   Action name
 */
 
action('acfe/form/submit/post',                $post_id, $type, $args, $form, $action);
action('acfe/form/submit/post/form=my-form',   $post_id, $type, $args, $form, $action);
action('acfe/form/submit/post/action=my-post', $post_id, $type, $args, $form, $action);
add_action('acfe/form/submit/post/form=my-form', 'my_form_submit', 10, 5);
function my_form_submit($post_id, $type, $args, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Do something...
    
    }
    
}

Query Var

After the action completion, a query var with the post arguments is automatically genereated. This query var is useful to retrieve the action information in the UI using {query_var:my-post-action:ID} or in PHP using get_query_var('my-post-action')['ID'].

If the action is named, the query var will simply use its name: my-post-action. If there is no action name, it will be named my-form-post . If there are multiple post actions without names, the second post query var will be called my-form-post-2 and so on.

The acfe/form/query_var/post hook allows you to change the data that is returned. Usage example:

/*
 * Post Query var
 * 
 * @array   $data     Post information
 * @array   $post_id  Created/Updated post ID
 * @string  $type     Action type: 'insert_post' or 'update_post'
 * @array   $args     Post arguments
 * @array   $form     Form settings
 * @string  $action   Action name
 */
 
filter('acfe/form/query_var/post',                $data, $post_id, $type, $args, $form, $action);
filter('acfe/form/query_var/post/form=my-form',   $data, $post_id, $type, $args, $form, $action);
filter('acfe/form/query_var/post/action=my-post', $data, $post_id, $type, $args, $form, $action);
add_filter('acfe/form/query_var/post/form=my-form', 'my_form_query_var', 10, 6);
function my_form_query_var($data, $post_id, $type, $args, $form, $action){
    
    /*
     * $data
     *
     * Array
     * (
     * [ID] => 1554
     * [post_author] => 1
     * [post_date] => 2020-08-19 20:53:31
     * [post_date_gmt] => 0000-00-00 00:00:00
     * [post_content] =>
     * [post_title] => My Post
     * [post_excerpt] =>
     * [post_status] => publish
     * [comment_status] => open
     * [ping_status] => open
     * [post_password] =>
     * [post_name] =>
     * [to_ping] =>
     * [pinged] =>
     * [post_modified] => 2020-08-19 20:53:31
     * [post_modified_gmt] => 2020-08-19 18:53:31
     * [post_content_filtered] =>
     * [post_parent] => 0
     * [guid] => /?p=1554
     * [menu_order] => 0
     * [post_type] => page
     * [post_mime_type] =>
     * [comment_count] => 0
     * [filter] => raw
     * [ancestors] => Array()
     * [page_template] =>
     * [post_category] => Array()
     * [tags_input] => Array()
     * [permalink] => /my-post
     * [admin_url] => /wp-admin/post.php?post=1554&action=edit
     * [post_author_data] => Array
     *      (
     *          [ID] => 1
     *          [user_login] => user
     *          [user_pass] => ##password_hash##
     *          [user_nicename] => user
     *          [user_email] => user@domain.com
     *          [user_url] =>
     *          [user_registered] => 2020-03-07 08:52:03
     *          [user_activation_key] =>
     *          [user_status] => 0
     *          [display_name] => User
     *          [nickname] => User
     *          [first_name] =>
     *          [last_name] =>
     *          [description] =>
     *          [rich_editing] => true
     *          [syntax_highlighting] => true
     *          [comment_shortcuts] => false
     *          ...
     *      )
     * )
     */
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Set a custom key
        $data['custom_key'] = 'custom_value';
    
    }
    
    // return
    return $data;
    
}