0

For a Custom Post Type I want to have the Title to always be the Authors Username. So far I've:

  • Limited each Author to only posting this type once so as to avoid conflicts.
  • Added code to the theme function to remove the Title box (code below)

    function remove_box() { remove_post_type_support('custom_post_type_name', 'title'); }
    add_action("admin_init", "remove_box");
    

Now I'm looking for a way in the theme function to insert the Author's username as the Title automatically. As currently newly added posts are given the name 'Auto Draft'. Any help would be appreciated.

0

There's actually some cool filters that allow you to pre-populate the title field and editor field, the one we need is default_title. Once in the hook, we need to get our user and display the name:

function post_author_title( $post_title, $post ) {
    if( $post->post_type == 'your_post_type' ) {
        $user = wp_get_current_user();

        $post_title = $user->display_name;
    }

    return $post_title;
}
add_filter( 'default_title', 'post_author_title', 10, 2 );

You can try the above but if the title box is removed I'm not sure if this will work. Another option is to manually add the title once the post is saved:

Answer Link

Author - chrisguitarguy

function wpse67262_change_title( $data ) {
    if( 'your_post_type' != $data['post_type'] )
        return $data;

    $user = wp_get_current_user();
    $data['post_title'] = $user->display_name;

    return $data;
}
add_filter( 'wp_insert_post_data', 'wpse67262_change_title' );

Hopefully one of those functions work for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.