Support

Account

Home Forums Backend Issues (wp-admin) Detecting what has just been saved when using save_post action

Solved

Detecting what has just been saved when using save_post action

  • I have a custom options page made in ACF and I need to run a function when this options page is saved. It needs to occur AFTER the actual updated acf data is saved.

    I understand I can use the sample acf_save code on the acf website (https://www.advancedcustomfields.com/resources/acfsave_post/) as a basis of an action. The issue is, how do I detect when using acf_save what has actually just been submitted because right now it’s running on every ACF update I do, on this settings page and also in other post types.

  • There isn’t any way to know what options page is being saved, normally. ACF passes the post ID to your filter for acf/save_post. For options pages this values is always ‘option’ or maybe it’s ‘options’, don’t remember exactly right now. The best you can hope for under this condition is running your function for every options page.

    When setting up an options page, acf has an option to use a specific post ID. This post ID can belong to any valid post. If you use the post ID option for the options page then you can tell when the specific options are being saved.

    I have a plugin that actually creates options pages. This plugin creates a custom post type to store information about the settings of each options page. There is also a setting that lets you save the values of fields on the options page to the post ID of the options page CPT. I have also used hidden post_mata names (they all begin with _) so that none of the settings that you enter will interfere with functions like get_fields(), or get_field_objects(). https://github.com/Hube2/acf-options-page-adder, even if you don’t use it you might get some ideas from looking at it.

  • Hi John thanks for the info. I’m just going to do

    function my_acf_save_post( $post_id ) {
        
        if ($post_id=="options"){
    	// my stuff to do...
        }
    	
    }

    and then go from there to keep it simple. I didn’t know about the post id appearing as options and I can confirm it’s plural, with the s. Cheers!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.