Auto populating post title with the post ID

written by: Jeff McNear

This is a very narrow use case. Recently a client wanted to have the title of a custom post type auto-populate with an incrementing number. In WordPress the post ID is an auto-incrementing (and unique) number attached to each piece of content. So after some deep digging I found this post: that provided the crucial block of code to be placed in the functions file of the active theme:

function wpd_default_title_filter( $post_title, $post ) {
    if( 'project_type' == $post->post_type ) {
        return 'Job #' .$post->ID;
    return $post_title;
add_filter( 'default_title', 'wpd_default_title_filter', 20, 2 );

In this case “project_type” is the post type where the auto incrementing is taking place and the post ID is prefaced by the term “Job #”.

The title field still remains editable, allowing the auto generated title to be over written.