Skip Navigation

[Resolved] Assign author name and post id to title through cred form

This support ticket is created 6 years, 10 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 6 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#602623

Tell us what you are trying to do?
I have a custom post type call nominations. For each nomination, I want the post title (which is not used in the cred form), to be assigned automatically. I would like to assign the post title the name of the author and then the post id.

Is there any documentation that you are following?
https://toolset.com/forums/topic/unique-urls-for-specific-cpts-created-though-cred/
https://toolset.com/forums/topic/trying-to-create-post-title-from-2-custom-fields/
This illustrates some aspect of this but it doesn't help me with the post title assigned to an author.

Here is the code I have generated so far:

function sf_save_data_action($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==8096){

//Declare the content of your variables, change "your_custom_field_slug" accordingly
$current_user = wp_get_current_user();
$first = $current_user->user_firstname;
$last = $current_user->user_lastname;
$custom_title = $last.' '.$first;

//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title
);

// Update the post into the database
wp_update_post( $my_post );

}
}
add_action('cred_save_data', 'sf_save_data_action',10,2);

The bit that I'm uncertain about is how to also add a post id or number so that each post has a unique id e.g. fred fox 1, fred fox 2, fred fox 3

#602681

Shane
Supporter

Languages: English (English )

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

Hi Gary,

Thank you for contacting our support forum.

Firstly does this code currently save the new title ? If it does then we can figure out a way to do the number on the title as well.

Please let me know.
Thanks,
Shane

#602690

Hi Shane,
this code works ok.

I've moved on now so that I have added a custom user field called max posts.

function sf_save_data_action($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==316093){

//Increment count and assign to post title
$current_user=wp_get_current_user();
$first = $current_user->user_firstname;
$last = $current_user->user_lastname;
$existing_value = get_user_meta($current_user, 'wpcf-max-posts', true);
$new_value = $existing_value;
$new_value = $new_value + 1;
update_user_meta( $current_user, 'wpcf-max-posts', $new_value);
$custom_title = $last.' '.$first.' '.$new_value;

//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title
);

// Update the post into the database
wp_update_post( $my_post );

}
}

So the code works but it doesn't seem to increment on the form. In other words I keep getting Fred Fox 1 as the post title and not Fred Fox 1, Fred Fox 2...

#602731

Shane
Supporter

Languages: English (English )

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

Hi Gary,

The problem is that we are not able to store the last value.

So what you can do as a workaround or a valid solution is to have a special custom field to hold the incremented value . This custom field can be stored in a separate post or CPT then you can retrieve that value with the get post meta, Increment it by 1 and update the current post title. Then store that incremented value back into the custom field.

Please let me know if this solution works for you.
Thanks
Shane