• Resolved bjusters

    (@bjusters)


    I would like to be able to run a function after the WordPress native save of a post is done, so the data is already in the db. I`d like to write data from an acf repeater field to a more flat tablet, so searching this data will be a lot faster.

    I searched through the forum and looked at the save_post hook, but it didn`t get clear to me how i can do the trick.

    Foed anyone have an idea on how to do make it work?

Viewing 6 replies - 1 through 6 (of 6 total)
  • As an example:

    function myplugin_save_post () {
        global $post;
    
    	if ((!defined ("DOING_AUTOSAVE") || !DOING_AUTOSAVE) && get_post_type ($post) == "my_post_type") { // Post type can also be page, post, etc
    		update_post_meta ($post->ID, "meta_key", $_POST ["meta_value"]);
    	} // if ()
    }
    
    add_action ("save_post", "myplugin_save_post");

    That’s all there is to it. You just need to add whatever code in there to process and save the data the way that you want it.

    Thread Starter bjusters

    (@bjusters)

    Thank you very much,

    however this is still triggering it before the native WordPress save right?

    ive "build" an ACF repeater field with 1 date field and four checkboxes, people fill in about 100 rows, thats why i wanted wordpress to do it`s thing first, and after that i can just use the ACF repeater field to put it in an easy usable array (instead of the ACF backend $_POST array)

    the idea that got to me just now, is figuring out how i can prioritize my save_post hook in that way, that its gets executed after the ACF save is done, in that way i can use it in my preferred way.

    thanks for the solution and the insight!

    No. It triggers after the post/page/etc is saved. Not before. WordPress does it’s stuff, and then performs this trigger.

    If you’re at all concerned you can look at the third parameter for the ‘add_action()’ function which is the priority. Set that higher and it will be processed last.

    Thread Starter bjusters

    (@bjusters)

    Grazie,

    i fiddled around with it like a year ago or something to move data to xml, to relieve the database, and i got the idea is was triggered lateron.

    I can see in the ACF code (that uses add_action save_post as well, didn`t think of that before either) this rule:

    add_action('acf/save_post', array($this, 'save_post_lock'), 0);
    add_action('acf/save_post', array($this, 'save_post'),10);
    add_action('acf/save_post', array($this, 'save_post_unlock'), 999);

    so ill just hook in at at least 1000 and ill be fine. Thanks for your wisdom, really appreciate it!

    To be honest. I have got no idea what the “acf” code is. That seems like it’s some external library to the standard WordPress actions.

    And.. yes it is.

    If you need help with that plugin I’d suggest that you ask in the plugins support forum. http://wordpress.org/support/plugin/advanced-custom-fields

    Thread Starter bjusters

    (@bjusters)

    The plugin wasn`t the problem, i was my lack of understanding of the hooks itself of which the plugin makes use as well.

    For all the future readers of this post:

    ACF stands for Advanced Custom Fields and has become an essential plugin for me in WordPress. (therefor ACF has become a normal verb in my dictionary:)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘run function after wordpress native save’ is closed to new replies.