Last active
October 6, 2017 04:36
-
-
Save schalkjoubert/d4ad042f0eac2dbcb990e5250b27cf05 to your computer and use it in GitHub Desktop.
Use ACF field to populate Title AND update slug
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
//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