Set Custom Post Type title to the Post’s Date

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.

Leave a Comment