Hi. I’m trying to figure this out. I have two custom post types. In function.php I’m using this snippet which makes ACF’s date field = publish date. Works fine. However, I have 2 post types, and the same snippet (edited variables and different field name) doesn’t work for both of them, only for the one placed in function.php first.
Ideally, what I want is when I create a new CPT and hit publish, that the publish date gets picked and set up based on the selection from the ACF date field. I’m assuming it’s something with the save_post (duplicate) but don’t know how to make it work.
The snippet I am using atm (to describe better I was trying the same thing for the other post type where I would change the field name and ofc function name and variables).
function news_date_acf_save_posts( $post_id ) {
// get new value
$theDate = get_field(‘news_date’, $post_id, false);
$theDate = date_create_from_format(‘Ymd’, $theDate);
$theDate = date_format($theDate, ‘Y-m-d H:i:s’);
$my_post = array(
‘ID’ => $post_id,
‘post_date’ => $theDate,
);
// do something
// unhook this function so it doesn’t loop infinitely
remove_action(‘acf/save_post’, ‘news_date_acf_save_posts’);
// update the post, which calls save_post again
wp_update_post( $my_post );
// re-hook this function
add_action(‘acf/save_post’, ‘news_date_acf_save_posts’);
}
// run after ACF saves the $_POST[‘acf’] data
add_action(‘acf/save_post’, ‘news_date_acf_save_posts’, 20);
This is what I’ve ended with, renamed properly but still, only the first one works, second doesn’t. In short, when a user creates a new post (post type) and selects via acf date field, date, hits publish, publish date should be picked up from the ACF date field and set like that. But this approach doesn’t update for either. However, think makes more sense going with cpt detect. Do note, the code below doesn’t actually work for any as I’ve detecting cpt first, so have to find a workaround for that as well.
function set_date_acf_save_posts( $post_id ) {
$post_type = get_post_type($post_id);
if ($post_type === ‘cpt1’) {
// get new value
$theDate = get_field(‘news_date’, $post_id, false);
$theDate = date_create_from_format(‘Ymd’, $theDate);
$theDate = date_format($theDate, ‘Y-m-d H:i:s’);
$news_post = array(
‘ID’ => $post_id,
‘post_date’ => $theDate,
);
// unhook this function so it doesn’t loop infinitely
remove_action(‘acf/save_post’, ‘ set_date_acf_save_posts’);
// update the post, which calls save_post again
wp_update_post( $news_post );
// re-hook this function
add_action(‘acf/save_post’, ‘ set_date_acf_save_posts’);
}
if ($post_type === ‘cpt2’) {
// get new value
$theDate = get_field(‘dealdate’, $post_id, false);
$theDate = date_create_from_format(‘Ymd’, $theDate);
$theDate = date_format($theDate, ‘Y-m-d H:i:s’);
$deals_post = array(
‘ID’ => $post_id,
‘post_date’ => $theDate,
);
// unhook this function so it doesn’t loop infinitely
remove_action(‘acf/save_post’, ‘ set_date_acf_save_posts’);
// update the post, which calls save_post again
wp_update_post( $deals_post );
// re-hook this function
add_action(‘acf/save_post’, ‘ set_date_acf_save_posts’);
}
}
// run after ACF saves the $_POST[‘acf’] data
add_action(‘acf/save_post’, ‘ set_date_acf_save_posts’, 20);
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.