[Resolved] Automatically add sequential value in a field when form is saved
This support ticket is created 4 years, 5 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.
I have a post form that uses a field "ID". I need this field to contain a sequential number for every post, based on the value used in the same field the last time the form was saved. If the last post's ID field was 282, the next time a post is created with the form the ID value should be 283.
I have tried to write code using the cred_save_data hook as follows, but it doesn't work. The form's ID is 18074, the slug of the post type is "trip-request" and the field in question is "ID"
add_action( 'cred_save_data', 'custom_save_func', 10, 2 );
function custom_save_func( $post_id, $form_data ) {
if ($form_data['id']==18074) {
// get the most recent trip requests posts
$promotion_args = array(
'numberposts' => 1,
'post_type' => 'trip-request',
'orderby' => 'post_date',
'order' => 'DESC'
);
$promotions = get_posts( $promotion_args );
// get the ID of the prior post
$last_id = get_post_meta( $promotions[1]->ID, 'wpcf-id', true );
// if there are no posts, start with 0
if ( !$last_id ) {
$last_id = 0;
}
// increment
$last_id = $last_id + 1;
// set the ID of the current post
update_post_meta( $post_id, 'wpcf-id', $last_id );
}
}
Hello. Thank you for contacting the Toolset support.
As the original ticket was resolved, our system automatically removes the access details you should.
I will require access details so that I can adjust your code.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Thanks for sharing access but it seems the access details you shared is for your production site.
And with the current access details I can see the following form on your site:
=> hidden link
And the form displayed with the above link is not using the Toolset form so I can not bind the cred_save_data hook to it.
Can you please share access details to the site where I can see the form created using the Toolset form so that I can hook the cred_save_data hook.
I have set the next reply to private which means only you and I have access to it.
I've adjusted the code within the cred_save_data hook you added where added your custom code as given under:
- hidden link
// get the most recent trip requests posts
$promotion_args = array(
'numberposts' => 1,
'post_type' => 'trip-request',
'orderby' => 'post_date',
'order' => 'DESC'
);
$promotions = get_posts( $promotion_args );
// get the project_id of the prior post
$last_id = get_post_meta( $promotions[0]->ID, 'wpcf-id', true );
if ( !$last_id ) {
$last_id = 0;
}
// increment
$last_id = $last_id + 1;
// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-id', $last_id );
I can see its working as expected. Can you please confirm.
It's doing the same thing it was doing earlier (not incrementing the value) but I just realized that when figuring out if what the existing ID is, it only considers published posts. So if several new draft posts are created, they will all get the same ID as the last published post.