Home Forums General Issues acf/save_post question


acf/save_post question

  • Hi,

    I have two custom post types both using ACF fields. I’m using a post_object to pull some information from one of the CPTs into the other (using a dropdown to figure out which post to use from the second CPT). I’d like to save some of the data from the second CPT as post_meta attached to the first CPT (the second CPT post may/may not exist in the future so rather than rely on it being there I’d like to save some of its data in the second CPT.

    I think I can use acf/save_post but I’d like to know how to handle scenarios where the post is updated versus saved the first time. I obviously don’t want to keep adding post_meta data to the DB and would like the existing meta to be updated in the event of a post being updated.

    Any help would be appreciated – especially example code.


  • why dont you use a repeater in each CPT called something like ‘relational meta’ and acf update_field.

    you can use wp noonce ajax to take data from the first CPT, pass it to your function (in functions.php) with ajax check if the second CPT exists, then check if the second CPT has the data you want to save, if not, save it. This is also benifical as you can detach your logic from wp concerning post meta and use ACF as your conduit.

    an added bonus is this is all done without reloading the page.

    this would be a mix of wp, acf, js and ajax. Ping if you require further assistance.

  • In response to the OP. In order to use acf/save_post and tell the difference between a new post and a post that’s being updated, you’ll need some other flag or marker that you can check prior to copying the information from the other post. For example:

    function my_acf_save_post($post_id) {
      if (get_post_type($post_id) != 'my-post-type') {
      if (get_post_meta($post_id, 'my-already-copied-flag', true) {
        // this one is already done
      // copy the values you want to copy
      // end with updating the flag field
      update_post_meta($post_id, 'my-already-copied-flag', 1);
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘acf/save_post question’ is closed to new replies.