Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.