-
-
Save philhoyt/11ab22518aa26fedeb89867a228ea53f to your computer and use it in GitHub Desktop.
<?php | |
/** 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 ); | |
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 try this gist.
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..
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; }
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?
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.
@Animalejourbano did you ever figure out how to use TWO ACF fields to generate your page title?
it works for me
// Get title from 'contacts'CPT acf field 'contact_name'
if (get_post_type($post_id) == 'post_zamestnanci') {
$employee_name = get_field("za_jmeno_zamestnance");
if ($employee_name) {
$prefix = $employee_name['za_title_before'];
$name_first = $employee_name['za_name_first'];
$name_last = $employee_name['za_name_last'];
$postfix = $employee_name['za_title_after'];
}
$new_title = get_field( $employee_name, $post_id ) . $prefix . ' ' . $name_first . ' ' . $name_last . '' . $postfix;
$new_slug = sanitize_title( $new_title );
wp_update_post(array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
));
}
return $value;
To get "Last Name, First Name" here's the code:
function contact_title( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) === 'contact' ) {
$new_title = $value . ', ' . get_field( 'first_name', $post_id );
$new_slug = sanitize_title( $new_title );
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', 'contact_title', 10, 3 );
I adapted this to my use case, and the title only updated if I updated the post twice.
I modified the code (changing $new_title = $value) and it updates the title after updating the post once , though I'm not sure why it works. If someone more experienced than me could explain why it works, or a better way to do it I would appreciate it!
`function review_title( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) === 'review' ) {
$new_title = $value;
$new_slug = sanitize_title( $new_title );
wp_update_post(
array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
)
);
}
return $value;
}
add_filter( 'acf/update_value/name=reviewer_name', 'review_title', 10, 3 );`
This works for me:
function acf_title_solicitudes( $value, $post_id, $field ) {
$new_title = get_field('servicio', $post_id). ' ' . $value;
$slug = sanitize_title( $new_title );
$postdata = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_type' => 'solicitudes',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=servicio', 'acf_title_solicitudes', 10, 3);
Source: https://www.jennybeaumont.com/auto-create-post-title-acf-data/
This worked initially, but then it started putting the field in twice. Also, how would the code look with three different custom post types?