Support

Account

Home Forums Front-end Issues How to duplicate post on acf_form save ?

Helping

How to duplicate post on acf_form save ?

  • Hi,

    I’m trying to duplicate an existing post on acf_form save.

    I tried this piece of code but it doesn’t work (actually, it delete the post).

    add_filter('acf/pre_save_post' , 'my_pre_save_post_ets', 10, 1);
    function my_pre_save_post_ets($post_id) {
    
      if (is_admin() || get_post_type($post_id) != 'etablissement') {
        return $post_id;
      }
      $args = array(
        'ID' => $post_id,
        'new_post' => array(
          'post_type' => 'etablissement',
          'post_status' => 'draft',
        )
      );
      $post_id = wp_insert_post( $args ); 
      return $post_id;
    }

    Do you have an idea for me ?

    Thanks in advance,
    Willwoodyy

  • First thing is that acf/pre_saved_post will only work with an acf_form() on the front end of the site and will not work in the admin so is_admin() is doing nothing for you. You’d need to do something completely different than using acf/pre_saved_post if you want this to happen in the admin as well and I don’t know what that is.

    Second issue is that in order to duplicate the post you need to omit the ‘ID’ value. $post_id already exists. According to the WP docs, this has the effect of updating the existing post rather than inserting the new post, so you are not changing the post ID.

Viewing 2 posts - 1 through 2 (of 2 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.