I am trying to generate a custom post title from two ACF fields. I have first_name
and last_name
but it will only generate the last name and does it twice:
/contacts/deere-deere/
Here’s my function:
function update_contacts_title( $value, $post_id, $field ) {
$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('last_name', $post_id). ' ' . $value;
$title = $first_name .' - '. $last_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);
If I modify this line down like so, then it will generate the first_name
but only once:
/contacts/john/
//Create new title based on ACF Fields
function update_contacts_title( $value, $post_id, $field ) {
$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('last_name', $post_id). ' ' . $value;
//$title = $first_name .' - '. $last_name;
$title = $first_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
//add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);
This is really confusing me as to why it won’t recognize both the first name and last name.
Any suggestions would be extremely helpful, thank you all.
2
Answers
Here's the answer I figured out after I remembered that I have the
first_name
andlast_name
in a group, which requires a repeater field in the function:You have "first_name" for both the names I believe.