Home › Forums › General Issues › Frontend Form not populating right in backend › Reply To: Frontend Form not populating right in backend
The acf/save_post
hook is executed every time a post or page is saved. That means that you can use only one function, but you need to make sure which post type you want to modify. Here’s an example how to do it in your case:
//Auto add and update Title field for English/Yes form:
function vbs_post_title_updater( $post_id ) {
// list the post type you want to modify
$post_types = array('en', 'ey');
// get the current post type
$current_post_type = get_post_type($post_id);
// if current post type is not in the list, don't do anything
if( !in_array($current_post_type, $post_types) ){
return;
}
// Set the title for 'en' post type
if( $current_post_type == 'en' ) {
// get the church name
$new_title_church_name = get_field('church_name', $post_id);
// get the selected date and the current date
$new_title_date = get_field('start_date', $post_id);
$new_title_date = new DateTime($new_title_date);
// set the title
$new_title = $new_title_church_name . ' - ' . $new_title_date->format('M j, Y');
// set the title for 'ey' post type
} elseif ( $current_post_type == 'ey' ) {
// get the church name and set it directly to the post title
$new_title = get_field('church_name2', $post_id);
}
$new_slug = sanitize_title( $new_title );
// create the update data holder
$vbs_ey_post = array();
$vbs_ey_post['ID'] = $post_id;
$vbs_ey_post['post_title'] = $new_title;
$vbs_ey_post['post_name'] = wp_strip_all_tags($new_slug);
//Unhook function to prevent infitnite looping
remove_action('acf/save_post', 'vbs_post_title_updater', 20);
// Update the post into the database
wp_update_post( $vbs_ey_post );
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'vbs_post_title_updater', 20);
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'vbs_post_title_updater', 20);
If it shows any error message, please let me know the message.
Hope this helps 🙂
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.