Home › Forums › General Issues › Update custom post title with ACF Fields
Hi, I have a custom post type called contacts
. Within that post type I have two fields:
1. First Name
2. Last Name
I have also hidden the default title
field via:
'supports' => ['author', 'revisions', 'thumbnail'],
and I added a meta box that publishes the permalink after the post is create:
function contacts_metabox_permalink() {
add_meta_box( 'prfx_meta', ( 'View Contact' ), 'contacts_metabox_permalink_callback', 'contacts', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'contacts_metabox_permalink' );
function contacts_metabox_permalink_callback( $post ) {
echo '<a href="';
the_permalink();
echo '" class="button button-small" target="_blank">';
echo the_permalink();
echo '</a>';
}
Now, what I am trying to accomplish is to allow the two ACF fields first_name
and last_name
to be used as the page title, so the final permalink URL would be something like:
/contacts/john-deere
This is a function I have found that works for the most part, however, it is only appending the last name (twice) to the post, like so:
/contacts/deere-deere
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=last_name', 'update_contacts_title', 10, 3);
What I am trying to do, is use $title
as the final result in the permalink.
Well, one of my issues is that the first_name
and last_name
fields are within a group! I forgot about that but I’m still having trouble building the function to work as a group.
Any help would be appreciated!
Here’s the answer:
function update_contacts_title($value, $post_id, $field) {
if( have_rows('contact_info') ):
while( have_rows('contact_info') ): the_row();
$first_name = get_sub_field('first_name', $post_id);
$last_name = get_sub_field('last_name', $post_id);
$title = $first_name . '-' . $last_name;
$slug = sanitize_title( $title );
endwhile;
endif;
$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=last_name', 'update_contacts_title', 10, 3);
Update:
The above answer will allow you to update previously established posts but not work for new posts, so the following function addresses that and allows for new posts and previous posts to modify the title from acf fields:
function update_contacts_title($post_id) {
if( have_rows('contact_info') ):
while( have_rows('contact_info') ): the_row();
$first_name = get_sub_field('first_name', $post_id);
$last_name = get_sub_field('last_name', $post_id);
$title = $first_name . '-' . $last_name;
$slug = sanitize_title( $title );
endwhile;
endif;
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
}
add_filter('acf/save_post', 'update_contacts_title', 10, 3);
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.