Skip to content

Instantly share code, notes, and snippets.

@jpmarchand
Last active June 1, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmarchand/8a1c387e8e464d1fdf35 to your computer and use it in GitHub Desktop.
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/
<?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