dth20986 2015-09-11 05:23
浏览 59

wp_update_post从一组IF / ELSE语句的内部或外部接收数组

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.

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

  • 写回答

1条回答 默认 最新

  • dqd3690 2015-09-11 06:17
    关注

    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 );
    
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失