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