Home › Forums › Backend Issues (wp-admin) › Generate post title from two ACF fields
This is probably so simple for you all…
I have a custom post type called Job Updates (slug is job_updates)
It has 3 fields…
Job – a taxonomy
Date – date picker
Update – WYSIWYG
I would like the title of the post to auto generate based on the Job + Date
So if the job is Sherman Water Damage, it would be Sherman Water Damage 6-1-2022
I’ve searched the forum and tried several different functions in the Snippets app, while swapping out their variables for mine, but none seem to work. I don’t know php, so that might be part of it.
// run function after acf updates fields
add_action('acf/update_post', 20);
function title_from_fields($post_id) {
// check for your post type
if (get_post_type($post_id) != 'job_updates')) {
return;
}
// get the post
$post = get_post($post_id);
// get the taxonomy field
$terms = get_field('job', $post_id);
// taxonomy fields return an array, use the first term
$term = $terms[0];
// get date field
$date = get_field('date', $post_id);
// set post title/slug
$title = $term->name.' '.$date;
$post->post_title = $title;
$post->post_name = sanitize_title($title);
// remove this filter to prevent infinite loop
remove_filter('acf/update_post', 20);
// update the post
wp_update_post($post);
// re-add this filter
add_action('acf/update_post', 20);
}
Thank you for this suggestion.
For some reason it throws a fatal error from line 5
if (get_post_type($post_id) != ‘job_updates’)) {
There is a typo in my code, the line should be
if (get_post_type($post_id) != 'job_updates') {
Can anyone point me in the right direction for a similar outcome but without the date. Instead i would just like to combine two ACF fields like for example “site_name – site_location”
I have tried manipulating the code above, but cannot get them to combine the two together.
Thanks in advance
code for two fields would not need to be complicated
$post->post_title = get_field('field-1', $post_id).' '.get_field('field-2', $post_id);
Thankyou for that. Would i just use the code as you have types it, or would i need to edit to $new_title?
$post->post_title = get_field('site_name', $post_id).' '.get_field('site_location', $post_id);
if ( get_post_type( $post_id ) == 'sites' ) {
<ul>
$new_title = get_field( 'site_name', $post_id );</ul>
$new_slug = sanitize_title( $new_title );
}
Got it to work by changing the post_title to new_title in your code.
Hopefully this is correct
Thanks a million!
Only problem now is i have to go into every single post, click edit and save or the title is just “(no title)”.
If i try to do this via quick edit, it doesnt update either.
Any solution to this anyone?
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.