Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a CPT called "profile" that only supports the editor and thumbnail. Each user is limited to posting just 1 profile.

I'm looking for a way to prefill the title and slug fields with the display name of the post author. As it is now, if I click on Publish, the post_status in the db is "Auto Draft" and the URL becomes "localhost/mytestsite/profile/auto-draft-1". It seems WP needs a title or else it won't be considered "Published".

I've checked several questions already posted here and this one seems like the one I need. Custom Post Type with Custom Title

But since I want the author's name and not values in a taxonomy or custom field, I don't know how to modify the code to reflect that.

I see that get_the_author() needs to be in The Loop.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You could add a hidden input into the page to pre-set the title field, because it won't be on the page(because the type doesn't support titles).

Slug is produced from the title, so you should only need add a title value.

Something like this should work(though untested)..

add_action( 'submitpost_box', 'hidden_type_title' );

function hidden_type_title() {
    global $current_user, $post, $post_type;

    // If the current type supports the title, nothing to done, return
    if( post_type_supports( $post_type, 'title' ) )
        return;

    ?>
    <input type="hidden" name="post_title"value="<?php echo esc_attr( htmlspecialchars( get_the_author_meta( 'display_name', $current_user->data->ID ) ) ); ?>" id="title" />
    <?php
}

Though i'd perhaps suggest adding further to the code and checking if the author display name is not empty, etc... it should be enough to work with..(or get you started at least).. :)

share|improve this answer
    
Thanks t31os! Works perfectly! –  annabwashere Jul 28 '11 at 18:58
    
Happy i could help.. :) –  t31os Jul 29 '11 at 12:42
    
@t31os Thanks for the snippet. submitpost_box is behaving a little weird, the title in the all posts list for my CPT, updates when i update the post twice. Why is it doing so? P.S I have changed the condition to if ($post->post_type == 'my_cpt') {... and add the hidden text field when it returns true –  JeyKeu Jul 12 '13 at 9:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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