Home › Forums › General Issues › Create/update post title from ACF fields
Hello together,
I want to set the post title myself with custom fields.
For this I had found this post here:
https://support.advancedcustomfields.com/forums/topic/createupdate-post-title-from-acf-fields/
I have created 2 custom plugin folders for my custom post types.
As long as only one plugin is active, this works, or always save with the higher priority.
First Plugin with priority 15
add_action('acf/save_post', 'mitarbeiter_acf_speichern',15);
function mitarbeiter_acf_speichern($post_id) {
$mein_post = array();
$mein_post['ID'] = $post_id;
$nachname = get_field('nachname');
$vorname = get_field('vorname');
$mein_post['post_title'] = $vorname . ' ' . $nachname;
// Save/Update Post
wp_update_post( $mein_post );
}
Second Plugin with priority 16
// Wird nachdem speichern ausgeführt
add_action('acf/save_post', 'kantine_acf_speichern',16);
function kantine_acf_speichern($post_id) {
$kantine_post = array();
$kantine_post['ID'] = $post_id;
$myvar = get_field('menu01');
$kantine_post['post_title'] = $myvar;
wp_update_post( $kantine_post );
}
Can anyone give me a hint?
You need to have conditions on when the title is generated. In your case the second action with a priority of 16 will always run second. Since the fields of the other post type do not exist get field returns NULL and set the title to nothing.
YOu need to do something like
if (get_post_type($post_id) == 'your-cpt-name')) {
// set post title
}
or
if (get_field('your-field-name', $post_id)) {
// the field has a title, set post title from this field.
}
Thank you John, it helped to check the field if it is empty.
if (get_field('your-field-name', $post_id)) {
// the field has a title, set post title from this field.
}
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.