-
-
Save iRajatDas/0291d9929732d902472a5e3346342da1 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 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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment