Last active
June 1, 2017 21:10
-
-
Save jpmarchand/8a1c387e8e464d1fdf35 to your computer and use it in GitHub Desktop.
Auto-populate post title field with post ID in WordPress. Source: http://support.advancedcustomfields.com/forums/topic/createupdate-post-title-from-acf-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 | |
//* Auto-populate post title field with post ID | |
add_action('save_post', 'customprefix_update_post_title' ); | |
function customprefix_update_post_title($post_id) { | |
$my_post = array(); | |
$my_post['ID'] = $post_id; | |
// Auto-populate post title field only if post type = post | |
if ( get_post_type() == 'post' ) { | |
$my_post['post_title'] = get_the_ID(); | |
} | |
// Avoid infinite loops | |
// https://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops | |
remove_action( 'save_post', 'customprefix_update_post_title' ); | |
wp_update_post($my_post); | |
add_action( 'save_post', 'customprefix_update_post_title' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment