Skip to content

Instantly share code, notes, and snippets.

@rveitch
Last active December 7, 2022 21:40
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save rveitch/9018669face1686e74aaa68026856f36 to your computer and use it in GitHub Desktop.
Save rveitch/9018669face1686e74aaa68026856f36 to your computer and use it in GitHub Desktop.
Update WordPress post title from an ACF field value on save. (Advanced Custom Fields)
<?php
/**
* Update Post Title from an ACF field value on post save.
*
* Triggers on save, edit or update of published posts.
* Works in "Quick Edit", but not bulk edit.
*/
function sync_acf_post_title($post_id, $post, $update) {
$acf_title = get_field('my_acf_field_name', $post_id); // NOTE: enter the name of the ACF field here
if ( $title ) {
$title = $acf_title;
} else {
$title = $post->post_title;
}
$content = array(
'ID' => $post_id,
'post_title' => $title
);
remove_action('save_post', 'sync_acf_post_title'); // prevent a loop
wp_update_post($content);
add_action('save_post', 'sync_acf_post_title');
}
add_action('save_post', 'sync_acf_post_title', 10, 3);
@rveitch
Copy link
Author

rveitch commented May 15, 2017

  • Add this function into your theme's functions.php file (or appropriate plugin file).
  • Update my_acf_field_name to the name of the ACF field you wish to use for the title.

@LukaHarambasic
Copy link

Thank you so much for this Gist!

I only had an problem with line 24, I removed it and now everything works.
I also added a check for the current CPT: Wordpress: ACF-Field as CPT Title

Thanks,
Luka

@nbaessler
Copy link

if ( $title ) { in line 24 doesn't return anything, this must be changed to if ( $acf_title ) { to check if the ACF fields are empty.

@aaroil3
Copy link

aaroil3 commented Feb 14, 2022

There is an option that this will work even if the JetEngine plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment