acf/save_post

Overview

This action is called when a page / post / options page / taxonomy / user / attachment is saved.

ACF uses this hook to save the $_POST['acf'] data with a priority of 10. This 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 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.