Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use ACF field to populate Title AND update slug
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_name'] = '';
$posttypes = array( 'accommodation', 'destination' );
$currentposttype = get_post_type($post_id);
if ( in_array( $currentposttype, $posttypes ) ) {
$my_post['post_title'] = get_field('title', $post_id);
//Unhook function to prevent infitnite looping
remove_action('acf/save_post', 'my_post_title_updater', 20);
// Update the post into the database
wp_update_post( $my_post );
// slug will be generated by WP based on post title
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'my_post_title_updater', 20);
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment