[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.
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.
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...
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