首页 > 解决方案 > Date format corrupted in sub fields when copying an ACF field

问题描述

I've created a CPT to store a show calendar.
An ACF field calendar has been created to store places and dates of the shows. It is a repeater composed by 2 subfields: date (type=date) and city (type=select).

When I'm trying to copy the "calendar" field from one post to another, using
update_field( 'calendar', get_field( 'calendar', $id_post_origin ), $id_post_destination );
the dates are corrupted.

I've tried to run a delete_field() before but the result isn't better.

e.g. if my original post is:
0 => array (size=2) 'city' => string 'Vesoul' (length=6) 'date' => string '02/09/2019' (length=10)
the destination post will be after copying:
0 => array (size=2) 'city' => string 'Vesoul' (length=6) 'date' => string '09/02/2019' (length=10)

Is it possible to specify a date format? To duplicate a field without modifying it?

Thank you

标签: wordpressadvanced-custom-fieldsacfpro

解决方案


正如 ACF 支持的 Kenny 所说,

get_field() 函数包含一个格式化参数,您将其设置为 false 以返回原始数据库值。对于日期字段,未格式化的字符串将采用 YYYYMMD 格式。

ACF get_field() 手册描述了这个$format_value参数,并提供了一个“获取没有格式的值”的例子。

就我而言,解决方案是get_field()像这样编辑调用:
$dates = get_field( 'calendar', $id_post_origin, false ); // false to retrieve raw data update_field( 'calendar', $dates, $id_post_destination );


推荐阅读