首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新post时创建重复条目

更新post时创建重复条目
EN

Stack Overflow用户
提问于 2022-06-04 16:37:16
回答 2查看 39关注 0票数 0

我正在使用这个代码片段来添加客户从自定义字段license_plate输入的自定义元,当他们向他们的帐户添加一个新的工具时。无论如何,无论出于什么原因第二次保存帖子-它将再次添加相同的元,...this不会发生第三次或第四次,它停止在两个条目,意味着重复-注意:当擦除重新保存时,它将最多显示牌照2x后,更新了很多次。我相信update_post部分的代码中需要有一些东西,但是我不知道如何对它进行编码。有人能帮我在这里,它可以检查是已经填写的职位标题之前,然后离开,如果这是真的。

我将cpt用于自定义posts,ACF用于字段。

代码语言:javascript
复制
// Auto-populate post title with ACF fields created before hand
function engx_auto_generate_vehicles_post_title( $value, $post_id, $field ) {

//$date = strtotime(get_field('license_plate', $post_id));
$license = get_field('license_plate', $post_id). ' ' . $value; //get the value of the meta inputed by customer

$title = $license;   //actual usage
//$formatted_date = date_i18n('d M Y', $date);                       //saved for use case time
//$title = $formatted_date .' - '. $license;                         // this was kept as example with multiple meta usage


$slug = sanitize_title( $title );                                   //save meta

$postdata = array(
     'ID'          => $post_id,
     'post_title'  => $title,                                       //Use the meta $title as the New auto generated Title for the newly created post the customer just created
     'post_type'   => 'vehicles',                                   //The Custom Post Type Name
     'post_name'   => $slug
);

wp_update_post( $postdata );

return $value;

}
//add_filter('acf/update_value/name=date', 'jb_update_postdata', 10, 3);
add_filter('acf/update_value/name=license_plate', 'engx_auto_generate_vehicles_post_title',     10, 3);     //acf hook from where to fetch this specific field data - is from acf documentation 

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2022-06-04 20:13:11

我发现了需要先验证是否为空的代码,如下所示

代码语言:javascript
复制
//replace 
wp_update_post( $postdata );

//with
if ( isset( $_POST['post_title'] ) && empty( $_POST['post_title'] ) ) {
  wp_update_post( $postdata );
}
票数 0
EN

Stack Overflow用户

发布于 2022-06-05 00:04:38

在创建新的或更新现有的车辆帖子时,最好使用acf/save_post来处理事件.

https://www.advancedcustomfields.com/resources/acf-save_post/

见下面的例子。

代码语言:javascript
复制
// process vehicle details when saving the post
add_action('acf/save_post', 'vehicle_save_post', 20);
    
// vehicles post type save action
function vehicle_save_post ($post_id) {
    
    // check we are on vehicles post type else return early
    if(get_post_type($post_id) <> 'vehicles') return;
    
    // get current saved value for license_plate acf field
    $license = get_field('license_plate', $post_id);
    
    // get our vehicle post object
    $vehicle = get_post($post_id);
    
    // temporally remove the action
    remove_action('acf/save_post', 'vehicle_save_post', 20);
    
    // update the current post object
    $vehicle->post_title = $license;
    $vehicle->post_name  = sanitize_title($license);
    
    // update post with updated vehicle object
    wp_update_post($vehicle);
    
    // do more updates here like update_post_meta etc...
    
    // re add the action
    add_action('acf/save_post', 'vehicle_save_post', 20);
    
    // finally, return
    return;
    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72501448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档