Skip Navigation

[Resolved] How to automatically add a sequential value in a field when form is saved

This support ticket is created 3 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.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by ericE-4 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1655215

I have a post form that uses a field "ID". I need this field to contain a sequential number for every post. The first time the form is saved this field should be 1, then the second post that it creates would have an ID of 2, etc.

Some time ago, a toolset support staff provide me this code but I never used it because I couldn't get the rest of the form to work. Now I'm forced to use Toolset forms for creating new posts. I implemented the code, but it does nothing.

add_action('cred_before_save_data_18074', 'before_save_data_for_form_with_id_18074',10,1);
function before_save_data_for_form_with_id_18074($form_data)
{
    if ( $post->post_type == 'trip-request' ) {
   
        // get the most recent promotion posts
        $promotion_args = array(
            'numberposts'       =>   2,
            '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[1]->ID, 'wpcf-id', true );
   
        if ( !$last_id ) {
            $last_id = 0;
        }        
        
        // increment
        $last_id++;
   
        // set the project_id of the current post
        update_post_meta( $post_id, 'wpcf-id', $last_id );
   
    }
}
#1655455

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I am not sure why you are using the following line of code as there is no $post object variable is available or defined.

if ( $post->post_type == 'trip-request' ) {

Based on the hook I see that you attached the hook to your form ID 18074: cred_before_save_data_18074 - so the hook will run form form ID 18074 onlyand a form can have only one post type attached at a time so I wonder why you added the check for post type condition:

if ( $post->post_type == 'trip-request' ) {

I think you should try to remove that conditional statement and check if the following code help you to resolve your issue:

add_action('cred_before_save_data_18074', 'before_save_data_for_form_with_id_18074',10,1);
function before_save_data_for_form_with_id_18074($form_data) {
    
          // get the most recent promotion posts
        $promotion_args = array(
            'numberposts'       =>   2,
            '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[1]->ID, 'wpcf-id', true );
    
        if ( !$last_id ) {
            $last_id = 0;
        }        
         
        // increment
        $last_id++;
    
        // set the project_id of the current post
        update_post_meta( $post_id, 'wpcf-id', $last_id );
    
    
}

However, if you do not have any issues, you can also use the Toolset Form's hook: cred_save_data instead cred_before_save_data.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1656529

I removed that condition as suggested and filled out the form again to generate a new post. The ID field was still not filled in.

This code may be all wrong—it was provided by another Toolset support rep, I did not write it. Is there a better way of achieving what I need? I notice a lot of people asking for the same thing (unfortunately none of the answers I found were understandable), so it's a shame that this is not a built-in function as it's something so typical (which is why virtually every form builder plugin offers a feature like this).

Whatever. How can I generate a unique, incremental number for each new post?

#1656593

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I will be happy to guide you with the correct code implementation with your form.

To ensure that where-on what form you want to implement the sequential value for your custom field. Can you please confirm you want to implement that on the following form submit: hidden link

or you have another form?

#1657855

Yes that is the form.

#1657883

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for confirmation.

I've added a better code that will minimise the performance issue as this method will not have to fire a query.

For example:
- As you can see with the following code - we are adding a new option "cred_form_18074_cf_id_value" and assign it a value 1 if the option "cred_form_18074_cf_id_value" is not found. If the option is found, we are adding +1 to the option value for option "cred_form_18074_cf_id_value".

add_action('cred_save_data', 'func_setting_custom_sequential_id_value',10,2);
function func_setting_custom_sequential_id_value($post_id, $form_data) {
 
    if ($form_data['id']==18074){
      
      $id_option_value = get_option('cred_form_18074_cf_id_value');
      if($id_option_value){
         	$id_option_value = $id_option_value + 1 ;
         	update_option('cred_form_18074_cf_id_value',$id_option_value);
            update_post_meta( $post_id, 'wpcf-id',$id_option_value );
    	} else {
      		add_option('cred_form_18074_cf_id_value',1);
            update_post_meta( $post_id, 'wpcf-id',1 );
       }
      
       }
}

I've added the code to the custom code section offered by Toolset:
=> hidden link

Can you please try and check if this works as when I try to click on "Next" button on first step of form it throwing the JS errors on browser's console and I could not able to move to next step.

#1657927

Thanks that works great. The JS errors were because I was working on the page at the time.

Hopefully this can be added as a standard feature so future users can avoid this.

#1659125

I found out the code you provided was ignoring other posts which may have already used the ID or are currently using an ID that needs to be continued with the next post. I couldn't re-open this ticket, so I created another: https://toolset.com/forums/topic/automatically-add-sequential-value-in-a-field-when-form-is-saved/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.