Support

Account

Home Forums General Issues ACF save post, check if new post

Solved

ACF save post, check if new post

  • Hi,
    i need to do something in my ACF save post hook only if it’s a new post but this does not seems to workd…
    thanks for help.

    function my_acf_save_post( $post_id ) {
     if( $post_id != 'new_post' ) {
         return $post_id;
       }
    do_something ();
    $my_post = array();
    $my_post['ID'] = $post_id;
    $my_post['post_title'] = 'test';
    wp_update_post( $my_post );
    
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
  • ACF does not pass this information. Post ID will always have an idea and will not be ‘new_post’ when acf/save_post is called.

    If you are using a front end form I would suggest using acf/pre_save_post.

    If this is an admin form then the only way to do this would be to use the built in wp save_post hook with a priority > 10 https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

  • so as i need it for frontend + backend, i must use save_post ?

  • may be that also
    if ( get_post_status ( $post_id ) ) {
    // returns false if post doesnt exist
    }

  • Post status will not tell you whether the post is new or not, this will only hold a registered status https://codex.wordpress.org/Post_Status.

    The post will always exist when acf/save_post is run because acf/save_post happens on ‘save_post’ with a priority of 10. At this point in the process the post is already save and/or updated.

    If you are going to be saving and updating from both the front and back ends then the only solution is to use save_post.

    Unless there is something else that you are doing that can tell you if it’s new or not. Something that you are doing to the post or the fields that you can test to see if it is already done or not. Or you could could create another wp meta field to hold a flag.

    
    function my_acf_save_post( $post_id ) {
      $flag = get_post_meta($post_id, 'my_flag_field', true);
      if ($flag == 'already done') {
        // this is not a new post
        return;
      }
      // set flag
      update_post_meta($post_id, 'my_flag_field', 'already done');
    
      // do other stuff to new post
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
  • oh yes this is smart !

  • This filter allows you to modify the $post_id before any custom field data is saved from the front end form (acf_form()). This filter is used in the tutorial Using acf_form to create a new post.

    This filter is not required to create a new post due to the new_post argument added to acf_form() in v5.0.0.

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

You must be logged in to reply to this topic.

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.