If your create_new_user_posts() function above works, try adding the below to your code; substitute your field keys and post type.
function pre_save_company_name($post_id){
if( empty($_POST['acf']) ) { return; }
$company = $_POST['acf']['THE COMPANY NAME FIELD KEY'];
return $company;
}
add_action('acf/save_post', 'pre_save_company_name', 21);
function company_name_as_post_title( $data, $postarr ) {
if ( ! $postarr['ID'] ) return $data;
if ( $postarr['post_type'] !== 'YOUR POST TYPE' ) return $data;
$post_id = $postarr['ID'];
$company = pre_save_company_name($post_id);
if( empty($company) ) { return $data; }
$data['post_title'] = $company;
return $data;
}
add_filter( 'wp_insert_post_data', 'company_name_as_post_title', 99, 2 );
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.
Thanks John,
Your solution solved the question as asked.
Additionally, the problems you mentioned helped me realize that my plugin was poorly designed. The ACF Options Page turned out to be a much better, and much faster solution for what I am trying to accomplish ( no more get_posts() in my plugin file ).
Cheers!
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.