wp_update_post recieving an array from within or outside of a set of IF / ELSE statements

I’m trying to update certain post type’s posts (testimonial, client, project), based on an Advanced Custom Field (ACF) field.

Version 1 generates the post in the correct post type, but the code repeats wp_update_post in every IF / ELSE statement. Version 2‘s code only has wp_update_post once, outside of the IF / ELSE; but in addition to posting in the correct post type, it also generates a blank entry in the ‘post’ post type.

Read More

In either case, looking at the post_id’s, for each post that is made, the post_id appears to increase by a factor of 3.

My question is; is this behaviour expected? So far as the difference in wp_update_post is concerned. Furthermore, is the post_id jump by a factor of 3 normal?

VERSION 1: wp_update_post within every IF / ELSE

// 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' ) {

    $name_field = $_POST['acf'][field_55749b0918a33]; // Name

    $my_post = array(
      'ID' => $post_id,
      'post_title' => $name_field,
      'post_name' => $post_id
    );

    wp_update_post( $my_post );

  } elseif ( get_post_type( $post_id ) == 'project' ) {

    $name_field = $_POST['acf'][field_557546848cfd9]; // Name

    $my_post = array(
      'ID' => $post_id,
      'post_title' => $name_field,
      'post_name' => $post_id
    );

    wp_update_post( $my_post );

   }

}

add_action('acf/save_post', 'my_post_title_updater', 1);

VERSION 2: wp_update_post after the IF / ELSE

// 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' ) {

    $name_field = $_POST['acf'][field_55749b0918a33]; // Name

    $my_post = array(
      'ID' => $post_id,
      'post_title' => $name_field,
      'post_name' => $post_id
    );

  } elseif ( get_post_type( $post_id ) == 'project' ) {

    $name_field = $_POST['acf'][field_557546848cfd9]; // Name

    $my_post = array(
      'ID' => $post_id,
      'post_title' => $name_field,
      'post_name' => $post_id
    );

   }

wp_update_post( $my_post );

}

add_action('acf/save_post', 'my_post_title_updater', 1);

How Posts Appear in phpMyAdmin
How Posts Appear in phpMyAdmin

Related posts

1 comment

  1. About Version 2, you have to make sure that a valid post type was processed at all. If get_post_type() didn’t match any of your IF statements, $my_post would be undefined, and an empty update will occur. You should therefore change your update statement to:

    if (isset( $my_post ))
      wp_update_post( $my_post );
    

Comments are closed.