Support

Account

Home Forums Backend Issues (wp-admin) acf/save_post Generating Duplicates

Solved

acf/save_post Generating Duplicates

  • Hi,

    I have a function that updates the post title, depending on the post type, allowing the title to be generated from ACF fields. A problem I’ve only just noticed however, is that whenever I create or update a post (testimonial, client, project), a blank post is generated in the generic ‘posts’ area.

    // ACF Auto Titles
    function my_post_title_updater( $post_id ) {
    	if ( get_post_type( $post_id ) == 'testimonial' ) {
    
    		$my_post = array();
    		$my_post['ID'] = $post_id;
    		$my_post['post_title'] = get_field( 'name', $post_id ) . ' - \'' . get_field( 'testimonial', $post_id ) . '\'';
        $my_post['post_name'] = $post_id;
    
    	} elseif ( get_post_type( $post_id ) == 'client' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'name', $post_id );
        $my_post['post_name'] = $my_post['post_title'];
    
      } elseif ( get_post_type( $post_id ) == 'project' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'name', $post_id );
        $my_post['post_name'] = $my_post['post_title'];
      }
    
      wp_update_post( $my_post );
      
    }
    
    add_action('acf/save_post', 'my_post_title_updater', 20);

    If anyone has any idea how I can stop this behaviour, it would be greatly appreciated.

    Thanks,
    Andy.

    Testimonials Post Type
    Testimonials Post Type

    How testimonials appear
    How testimonials appear

    Generic post type
    Generic post type

    The blank posts that are generated
    The blank posts that are generated

  • Are you sure this function is casing the problem? Does the problem go away if you comment out the add_action line?

    If this function is causing the problem the only thing I can think of by looking at this function is that in may have to do with the ‘post_name’ value. Try commenting out those lines and see what happens.

  • I commented out the action and it no longer repeats (when editing a ‘testimonial’ post type), so it must be something in the function.

    I have started changing the function to run ahead of $_POST[‘acf’], but the issue remains. Please see my updated code below…

    // ACF Auto Titles
    function my_post_title_updater( $post_id ) {
      if ( get_post_type( $post_id ) == 'testimonial' ) {
    
        $name_field = $_POST['acf'][field_556ea5ac8c15b]; // Name
        $testimonial_field = $_POST['acf'][field_556ea5c68c15c]; // Testimonial
    
        $my_post = array(
          'ID' => $post_id,
          'post_title' => $name_field . ' - \'' . $testimonial_field . '\'',
          'post_name' => $post_id
        );
    
      } elseif ( get_post_type( $post_id ) == 'client' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'name', $post_id );
        $my_post['post_name'] = $my_post['post_title'];
    
      } elseif ( get_post_type( $post_id ) == 'project' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'name', $post_id );
        $my_post['post_name'] = $my_post['post_title'];
      }
    
      wp_update_post( $my_post );
      
    }
    
    add_action('acf/save_post', 'my_post_title_updater', 1);
  • OK… I’ve commented out the two ‘elseif‘ elements (as they are not currently setup for dealing with variables pre $_POST[‘acf’]). But what I’ve done is move the wp_update_post( $my_post ) to within the if statement. Can anyone confirm this is expected behaviour?

    Also, I’ve noted that the post_id’s when I’m adding new ‘testimonials’ are going up in 3’s. I.e. 84, 87, 90. Is this also expected behaviour, or indicative of an issue?

    // ACF Auto Titles
    function my_post_title_updater( $post_id ) {
    
      if ( get_post_type( $post_id ) == 'testimonial' ) {
    
        $name_field = $_POST['acf'][field_556ea5ac8c15b]; // Name
        $testimonial_field = $_POST['acf'][field_556ea5c68c15c]; // Testimonial
    
        $my_post = array(
          'ID' => $post_id,
          'post_title' => $name_field . ' - \'' . $testimonial_field . '\'',
          'post_name' => $post_id
        );
    
        wp_update_post( $my_post );
    
      /* } elseif ( get_post_type( $post_id ) == 'client' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'name', $post_id );
        $my_post['post_name'] = $my_post['post_title'];
    
        wp_update_post( $my_post );
    
      } elseif ( get_post_type( $post_id ) == 'project' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'name', $post_id );
        $my_post['post_name'] = $my_post['post_title'];
    
        wp_update_post( $my_post );
    
      */ }
      
    }
    
    add_action('acf/save_post', 'my_post_title_updater', 1);
  • Going up by 3 would depend on what else is being added. Attachments are posts as well.

    I’m not sure about using this before acf, or why this is happening to be honest.

    Is this happening in the admin or is this on a front end form? The reason I ask is that you’re trying to update a “testimonial” post type but the empty posts are showing up in the “post” post type.

  • It’s all backend and it’s only the ACF data being changed that makes it go up by 3.

    That said, with the exception of the post number jumping by 3, moving the wp_update_post( $my_post ); within each if/else statement; seems to have fixed the initial duplicates issue.

  • That said, with the exception of the post number jumping by 3, moving the wp_update_post( $my_post ); within each if/else statement; seems to have fixed the initial duplicates issue.

    Actually, that makes sense now that you say it.

    What was happening was that if the post type was not testimonial, client, or project then you were calling wp_update_post() with an empty array, so WP inserted and empty “post”.

    Sometimes it’s easy to overlook the simple things.

  • Also, so far as the post_id jumping by 3 is concerned; I think each revision creates a new post_id. And whilst I’m only clicking ‘Publish’ once, three different ‘revisions’ are being made.

    • Initial Save
    • wp_update_post
    • Save Action
  • This is what it looks like in the wp_posts table for a single post being published.

    This is what it looks like in the wp_posts table for a single post being published.

  • I wouldn’t worry about the post id and the revisions most of the time, although I generally tend to turn of auto saving and install a plugin to limit the number of revisions that are saved just to keep the db size down.

  • old topic, but you can also just add this to wp-config.php instead of adding another plugin.

    // limit revisions to prevent db bloating
    define('WP_POST_REVISIONS', 3);
Viewing 11 posts - 1 through 11 (of 11 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.