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 custom post type, called 'status' and I have disabled 'title' in the post type. Right now it's setting all posts to have the title "Auto Draft". I want to set up a way to set the title to be the post's date. I think I should be using 'wp_insert_post_data". Right now I am using the following code but it is not working:

function status_title_filter( $data , $postarr )
{

if ($postarr['post_type'] == 'status'){
    $date = $data['post_date'];
    $data['post_title'] = $date;
}
return $data;
}

add_filter( 'wp_insert_post_data' , 'status_title_filter' , '99' );

Thanks in advance for solving my simple problem. I'm a sort of novice when it comes to this kind of things.

share|improve this question
    
Hi.. I think you might be able to use the code found here to solve your problem: wordpress.stackexchange.com/questions/7829/… I was having a problem similar to yours (trying to set the post title to a specific taxonomy term). I am still having some trouble with mine though, as it sets the post-title but doesn't update the permalink. My question is posted here: wordpress.stackexchange.com/questions/22537/… Good Luck! –  Duiwel Jul 10 '11 at 22:24

3 Answers 3

Despite what the Codex says, $postarr doesn't always get passed in, so you should just use $data. $data isn't a meaningful variable name, though, so I prefer $cleanPost. I'd also try removing the priority on the filter, since it's not usually necessary. It's also a good idea to set the slug (post_name) in addition to the title, and to avoid running the code on auto-drafts and trash/untrash operations. See if this works instead:

function status_title_filter( $cleanPost )
{
    if( $cleanPost['post_type'] == 'status' && $cleanPost['post_status'] != 'auto-draft' && $_GET['action'] != 'trash' && $_GET['action'] != 'untrash' )
        $cleanPost['post_title'] = $cleanPost['post_name'] = $cleanPost['post_date'];

    return $cleanPost;
}

add_filter( 'wp_insert_post_data', 'status_title_filter' );

Update: I think the reason you don't see $postarr is because by default action callbacks only accept one parameter. If you want more then you have to specify that in the hook, like add_action( 'wp_insert_post_data', 'my_callback_function', 10, 2). That would set the priority to 10 and the number of arguments to 2.

share|improve this answer

There's a plugin that does that - datetitle. Maybe you could look at the code there and get an idea how to do it.

share|improve this answer

I know it's a late, but you can set the title to the postdate right before you create the post.

$postarr['title'] = current_time('mysql');
$postarr['post_type'] = 'myposttype';
wp_insert_post($postarr);

HTH

share|improve this answer

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.