Below is the code to auto-populate the post title field with its post ID in WordPress.
<?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' ); | |
} |