Skip Navigation

[Resolved] Saving user first name/last name as post title + post date/time

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the post author's name with post date and time to automatically set a new post title.

Solution: Use the save_post hook provided by WordPress to automate the post title. Get author information using get_the_author_meta, concatenate the title string, and use wp_update_post to update the post title. Example code:

function md_checkout_title( $post_id ) {
  if( 'material-check-out' == get_post_type($post_id) ) {
    remove_action( 'save_post', 'md_checkout_title' );
    $date = date( 'Y-m-d H:i:s', time() );
    $author_id = get_post_field( 'post_author', $post_id );
    $first_name = get_the_author_meta( 'first_name', $author_id );
    $updated_data = array(
      'ID' => $post_id,
      'post_title' => $first_name . $date
    );
    wp_update_post( $updated_data );
    add_action( 'save_post', 'md_checkout_title' );
  } else {
    return;
  }
}
add_action( 'save_post', 'md_checkout_title' );

Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_update_post/
https://developer.wordpress.org/reference/hooks/save_post/

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 22 replies, has 3 voices.

Last updated by Arrien 4 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1455027

Hi Christian,

Yes, I realize that what I am trying to accomplish now no longer uses the cred_save_data hook. I came to a point where I didn't see the point in having front end forms if admin are the only ones filing them out.

I've tried this code in the functions/php file to make sure that the MD Check-outs CPT saves the author name, date and time as the title, but only the date and time seem to be saving.

function md_checkout_title( $data ) {
if( 'material-check-out' != $data['post_type'] )
return $data;

$author = get_the_author_meta( 'first_name' );
$date = date( 'Y-m-d H:i:s', time() );
$data['post_title'] = $author . $date;

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

I feel quite over my head as I am not a programmer, but I did seem to get the date and time working through a lot of trial and error.

Am I missing something?

#1456275

The wp_insert_post_data hook is triggered before the post is created, so functions like get_the_author_meta will not work - there is no post yet. I would try the save_post hook instead.

#1458363

Hi Christian,

I tried this but now I'm getting post titles as Autosave

function md_checkout_title( $data ) {
if( 'material-check-out' != $data['post_type'] )
return $data;

$author = get_the_author_meta( 'first_name' );
$date = date( 'Y-m-d H:i:s', time() );
$data['post_title'] = $author . $date;

return $data;
}

add_action('save_post', 'md_checkout_title', 10, 2);

#1458489

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

Christian, is currently out today but will be back tomorrow to continue to help you resolve this issue.

Thank,
Shane

#1467667

Hi, the save_post hook does not automatically update the title, you must use wp_update_post inside the save_post hook to do that. You can see another ticket in the forum showing the syntax for updating a post here: https://toolset.com/forums/topic/submitting-editing-content-with-cred-and-changing-post-title-and-slug/#post-766993

#1467881

Hi Christian,

I tried this but I get an error:

function md_checkout_title( $data ) {
if( 'material-check-out' != $data['post_type'] )
remove_action( 'save_post', 'update_post_slug' );
$updated_data = array(

$author = get_the_author_meta( 'first_name' );
$date = date( 'Y-m-d H:i:s', time() );
$data['post_title'] = $author . $date;

);
wp_update_post( $updated_data );
add_action( 'save_post', 'md_checkout_title' );
} else {
return;
}
}
add_action( 'save_post', 'md_checkout_title' );

#1471757

Okay looks like you're struggling a bit with this, so let me give you some more direct guidance.

1. The md_checkout_title function's first callback parameter will be $post_id, not $data:

function md_checkout_title( $post_id ) {

2. You want to update material-check-out posts, correct? If so, then your "if" should test if the post type is equal to 'material-check-out'. Your current "if" tests if the post type is NOT equal to 'material-check-out'.
3. Since you have access to $post_id, you should check the post type using get_post_type with the post ID. You will not be able to find the post type by looking in an array of $data.
4. You must include an opening curly brace at the end of the "if" line, since there are multiple lines of code to execute inside the "if" clause.

if( 'material-check-out' == get_post_type($post_id ) ) {

5. When you call remove_action, you should include the name of your function 'md_checkout_title' instead of 'update_post_slug', which was the name of the function in another example.

remove_action( 'save_post', 'md_checkout_title' );

6. Array syntax requires keys to be surrounded in quotes. It requires commas instead of semi-colons separating each line, and it requires a double-arrow assignment operator "=>" instead of "=".
7. You must include the post ID as one of the array attributes so wp_update_post knows which post to update.
8. The post array keys you can use for wp_update_post are explained in the documentation for wp_insert_post (link below). The ID field should be 'ID', and the title you have correctly as 'post_title'.
9. You cannot define variables like $date and $author inside an array definition. You should define the variables outside the array first, then use those variables in the array attribute values.
10. You should add a user_id parameter when calling get_the_author_meta since this code is outside The Loop.

$date = date( 'Y-m-d H:i:s', time() );
    $author_id = get_post_field( 'post_author', $post_id );
    $first_name = get_the_author_meta( 'first_name', $author_id );
    $updated_data = array(
      'ID' => $post_id,
      'post_title' => $first_name . $date
    );
    wp_update_post( $updated_data );

Putting it all together:

function md_checkout_title( $post_id ) {
  if( 'material-check-out' == get_post_type($post_id) ) {
    remove_action( 'save_post', 'md_checkout_title' );
    $date = date( 'Y-m-d H:i:s', time() );
    $author_id = get_post_field( 'post_author', $post_id );
    $first_name = get_the_author_meta( 'first_name', $author_id );
    $updated_data = array(
      'ID' => $post_id,
      'post_title' => $first_name . $date
    );
    wp_update_post( $updated_data );
    add_action( 'save_post', 'md_checkout_title' );
  } else {
    return;
  }
}
add_action( 'save_post', 'md_checkout_title' );

Documentation for the save_post hook:
https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops

Documentation for the array keys to use in wp_update_post (the same as wp_insert_post):
https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters

#1473921

Thank you, Chrisitan.

I was even able to apply the code to other CPTs.

Thank you again!