Skip to content

Instantly share code, notes, and snippets.

@haveigonemental
Forked from rveitch/sync_acf_post_title.php
Last active July 19, 2020 00:03
Show Gist options
  • Save haveigonemental/f0209f685856429a8ae85b5c6684aca9 to your computer and use it in GitHub Desktop.
Save haveigonemental/f0209f685856429a8ae85b5c6684aca9 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 Custom 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_type = get_post_type($post_id);
if($post_type === "custom_post_type") {
$title = 'get_field('ACF_title_field', $post_id);
} else if($post_type === "other_custom_post_type") {
$title = get_field('ACF_title_field', $post_id));
} else {
//if no matching CPT do nothing
return;
}
$content = array(
'ID' => $post_id,
'post_title' => $title
);
wp_update_post($content);
}
add_action('acf/save_post', 'sync_acf_post_title');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment