wordpress - 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
解决方案
正如 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 );
推荐阅读
- c++ - Qt with visual studio , Qt 需要 c++17 编译器
- javascript - 如何从特定索引开始循环遍历数组(对象数组)并在Javascript中更改对象中的属性?
- python - PySpark:RDD如何减少字典列表上的关键字?
- youtube-api - Youtube 数据 API 活动端点不返回任何项目
- r - 如何在我生成的表中插入新列
- angular - 如何在 Synfusion Angular Scheduler 上通过 Ajax 将数据发布到自定义 API
- python - 如何使用 smtplib 和 EmailMessage 将 Bootstrap 添加到 python 电子邮件
- python - 类实例方法的Python记忆,我们可以/应该使用functools缓存吗?
- mysql - SQL创建表之间ID相互关联的表
- xamarin.forms - 在 Xamarin Forms 5 中重置单选按钮