Skip Navigation

[Resolved] Auto ID numbers duplicating

This support ticket is created 3 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 3 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1965009
toolsetidnumbersduplicating.png

Hello!

function auto_assign_customer_ids( $post_id, $post, $update ) {
  if( $update == false ) {
    // Only assign ID to new telorder posts
    if ( $post->post_type == 'customer' ) {
 
      // get the most recent telorder post
      $customer_args = array(
      'numberposts' => 1,
      'post_type' => 'customer',
      'orderby' => 'post_date',
      'order' => 'DESC'
      );
      $customers = get_posts( $customer_args );
 
      // get the project_id of the prior post
      $last_id = isset($customers[0]) ? get_post_meta( $customers[0]->ID, 'wpcf-customer-id', true ) : 0;
 
      if ( !$last_id ) {
$last_id = 0;
}
      
      // increment
      $last_id++;
 
      // set the project_id of the current post
      update_post_meta( $post_id, 'wpcf-customer-id', $last_id );
 
    }
  }
 
}
add_action( 'save_post', 'auto_assign_customer_ids', 100, 3 );

This is the code I am using, however after a while, ID numbers start to duplicate, please see screenshot.
I found this code on toolset.com and wondered if you had come accross this before?

#1965199

Hello,

I assume you are going to setup an auto-increment custom field.

If it is, there isn't such kind of built-in feature within Toolset plugins, you can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

And I have searched it in Google, found some related threads:
https://wordpress.stackexchange.com/questions/242789/how-to-add-a-custom-field-and-incrementing-value-for-all-the-published-posts
https://wordpress.stackexchange.com/questions/295630/how-to-auto-increment-post-title-post-slug-field

For your reference.