Home › Forums › General Issues › Auto-generating post_title with acf_form() › Reply To: Auto-generating post_title with acf_form()
Here is how I would go about it, untested code.
First a function to extract the names from $_POST, by adding an action to acf/save_post()
function pre_save_user_fullname($post_id){
if( empty($_POST['acf']) ) { return; }
$firstname = $_POST['acf']['FIRST NAME FIELD KEY'];
$lastname = $_POST['acf']['LAST NAME FIELD KEY'];
$fullname = $firstname.' '.$lastname;
return $fullname;
}
add_action('acf/save_post', 'pre_save_user_fullname', 21);
Then modifying the post data before it is written to db, by filtering wp_insert_post_data();
function user_fullname_as_post_title( $data, $postarr ) {
if ( ! $postarr['ID'] ) return $data;
if ( $postarr['post_type'] !== 'YOUR_POST_TYPE' ) return $data;
$post_id = $postarr['ID'];
$fullname = pre_save_user_fullname($post_id);
if( empty($fullname) ) { return $data; }
$data['post_name'] = $fullname;
return $data;
}
add_filter( 'wp_insert_post_data', 'user_fullname_as_post_title', 99, 2 );
If it doesn’t work, I hope it at least 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.