Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Advanced Custom Fields to create your Post Title
// Create Title and Slug
function acf_title( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) == 'staff' ) {
$new_title = get_field('first_name', $post_id) . ' ' . $value;
$new_slug = sanitize_title( $new_title );
// update post
wp_update_post( array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
) );
}
return $value;
}
add_filter( 'acf/update_value/name=last_name', 'acf_title', 10, 3 );
@Captaindrew80

This comment has been minimized.

Copy link

@Captaindrew80 Captaindrew80 commented Jan 17, 2019

This worked initially, but then it started putting the field in twice. Also, how would the code look with three different custom post types?

@logichub

This comment has been minimized.

Copy link

@logichub logichub commented Jan 18, 2019

@Captaindrew80 try this gist.

@jacobseated

This comment has been minimized.

Copy link

@jacobseated jacobseated commented May 16, 2019

Where should this file be placed?

Here is how I got it to work:

In case you are wondering where to place the code, you need to place it in your Theme's "functions.php" file. I suggest placing it at the bottom. Alternatively you can also install the "Code Snippets" plugin, as this will allow you to place code on your site without editing the theme files.

The above code also seem to have a bug. Once you hit save the first time, the title will be correctly updated. However, next time you update it, the title will be duplicated. I am no expert, but the below fixed it for me:

if (empty($value)) { $new_title = get_field('first_name', $post_id); } else { $new_title = $value; }

It's amazing Advanced Custom Fields does not support this functionality by default, but that is the just the state of things I guess..

@Noz5

This comment has been minimized.

Copy link

@Noz5 Noz5 commented Aug 7, 2019

The above code also seem to have a bug. Once you hit save the first time, the title will be correctly updated. However, next time you update it, the title will be duplicated. I am no expert, but the below fixed it for me:

if (empty($value)) { $new_title = get_field('first_name', $post_id); } else { $new_title = $value; }

@jacobkri
you need to add this line as well, to avoid slug duplication.

if (empty($value)) { $new_slug = sanitize_title( $new_title ); } else { $new_slug = $value; }

@Animalejourbano

This comment has been minimized.

Copy link

@Animalejourbano Animalejourbano commented Aug 15, 2019

I can't get this to work with multiple fields as the title. I have a field with lastname, one with name, and one with ID. I need the title to be for example:
Doe Joe 12312443242, and the slug doe_joe_12312443242

But I can't contactenate those fields in this function. Any help?

@easaw

This comment has been minimized.

Copy link

@easaw easaw commented Sep 2, 2020

A bit late to the party, but I found that the simple answer is to change this

$new_title = get_field('first_name', $post_id) . ' ' . $value;

to this:

$new_title = get_field('first_name', $post_id);

This way you only get the new, updated field as the post title and post slug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.