Hi @acf-support
I’m not running any other plugins. Only the latest version or WordPress and the latest version of ACF.
Ok, update_field() wasn’t working for me, even though I’m sure it was right. I am instead using $wpdb to update the field in the database, which is working. I’m not marking this as resolved as the issue with update_field() still remains. If anyone could shed any light on this please let me know.
I have now narrowed down the issue to update_field(). Here is mine:
update_field('field_552cb019acd87',$seminarDate,353);
This isn’t working. $seminarDate is a timestamp. Can anyone see any reason why this wouldn’t work?
Update:
@jonathan in my DB ACF is saving the datetime as a UNIX timestamp. I have edited the code to use update_field() and it still isn’t saving to the database, using the field name or field key.
function updateSeminarTime($post_id)
{
// bail early if no ACF data
if (empty($_POST['acf'])) {
return;
}
// array of field values
//$fields = $_POST['acf'];
// specific field value
$post = get_post($post_id);
$expertId = $post->post_author;
$userTimeZone = getTimeZone($expertId);
$userTimeZoneOffset = get_timezone_offset($userTimeZone, 'UTC');
$seminarDate = $_POST['acf']['seminar_date'];
$seminarDate = strtotime($seminarDate);
$seminarDate = $seminarDate + $userTimeZoneOffset;
//$seminarDate = date('m/d/y h:mm tt', $seminarDate);
echo $seminarDate;
echo $userTimeZoneOffset;
echo $post_id;
update_field('field_552cb019acd87',$seminarDate,$post_id);
}
// run before ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'updateSeminarTime', 20);
When I save the post, it automatically takes me to the new post. How can I debug this and see any echo’d values etc?
Hi @jonathan
I was trying to intercept the form submission before it was actually written to the DB using the hook/filter above. Is that not what this filter is for? I know it is doing something as the datetime is no longer written into the DB, it is just blank. Do I then need to update (SQL) that field? Seems like there should be a way to simply update the $_POST['acf']['seminar_date']
before it is inserted into the DB.
Update: So I have found this http://www.advancedcustomfields.com/resources/acfsave_post/.
I am hooking into it, but the date isn’t actually being written to the database. Not sure why not. Here is my code:
function updateSeminarTime( $post_id ) {
// bail early if no ACF data
if( empty($_POST['acf']) ) {
return;
}
// array of field values
//$fields = $_POST['acf'];
// specific field value
$post = get_post( $post_id );
$expertId = $post->post_author;
$userTimeZone = getTimeZone($expertId);
$userTimeZoneOffset = get_timezone_offset($userTimeZone,'UTC');
$seminarDate = $_POST['acf']['seminar_date'];
$seminarDate = strtotime($seminarDate);
$seminarDate = $seminarDate + $userTimeZoneOffset;
$seminarDate = date('m/d/y h:mm tt',$seminarDate);
$_POST['acf']['seminar_date'] = $seminarDate;
}
// run before ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'updateSeminarTime', 1);
I’m wondering if I am writing the format wrong. BTW I have tried with date() and formatting it to a string and also just as a UNIX timestamp.
Hi @dacrosby Thanks for your suggestion. Is any one else aware of another method? This way seems like a bit of a hack.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.