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