-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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