acf/save_post

Overview

This action is called when saving the submitted $_POST data.

This filter allows you to hook in before or after the data has been saved. It is important to note that the get_field() function will return different values at these times (previous value / new value).

Changelog

  • Changed $_POST key from ‘fields’ to ‘acf’ in version 5.0.0
  • Since version 4.0.0

Parameters

  • $post_id (int|string) the ID of the post (or user, term, etc) being saved

Usage

Before

This example shows how to hook into the acf/save_post action before ACF has saved the $_POST data. This is possible by using a priority less than 10.

<?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'];
    
}

add_action('acf/save_post', 'my_acf_save_post', 1);

?>

After

This example shows how to hook into the acf/save_post action after ACF has saved the $_POST data. This is possible by using a priority greater  than 10.

<?php

function my_acf_save_post( $post_id ) {
    
    // get new value
    $value = get_field('my_field');
    
    
    // do something
    
}

add_action('acf/save_post', 'my_acf_save_post', 20);

?>

Notes

parameters

Unlike the WP save_post action, this function does not contain the $post and $updated parameters. If you require access to these parameters, consider using the save_post action instead.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.