Home › Forums › Backend Issues (wp-admin) › Generate post title from two ACF fields › Reply To: Generate post title from two ACF fields
// 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);
}
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.