Skip Navigation

[Resolved] Auto ID number not updating correctly.

This support ticket is created 4 years, 3 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by SteBlood 4 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1805849
s1.png

I used this code which I found on these support forums;

function auto_assign_ids( $post_id, $post, $update ) {
   
    // Only assign ID to new approved promotion posts
    if ( $post->post_status == 'publish' && $post->post_type == 'viewed-caravan' ) {
   
        // get the most recent promotion posts
        $product_args = array(
            'numberposts'       =>   2,
            'post_type'         =>   'viewed-caravan',
            'orderby'           =>   'post_date',
            'order'             =>   'DESC'
        );
        $products = get_posts( $product_args );
   
        // get the project_id of the prior post
        $last_id = get_post_meta( $products[1]->ID, 'wpcf-product-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-product-id', $last_id );
   
    }
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );

It works great, apart from the fact that the ID numbers update to the 'lastest' created ID number when modifying the post.

Do you know if I've maybe done something wrong?

Many Thanks

#1806211

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hayley,

Thank you for getting in touch.

If you are updating the post I would suggest that you add a conditional in your code to check if field has a value in it before updating the field.

This will help to prevent your posts from getting the latest ID added to their custom field because the check will ensure that only new posts will get the value added.

Thanks,
Shane

#1807411

This is cheeky, I know!
Do you know what the condition code is that I could add?

If not that’s fine. I’ll have a look on Google and find an answer 😁

You guys are already great!

#1808993

So, I had a further look on these forums for an answer.

https://toolset.com/forums/topic/number-will-increment-when-editing-post-auto-numbering-posts-vs2/
Christian provides the following code;

/**
* Add an auto-incrementing ordernummer field to bpark posts
*/
function auto_assign_bpark_ids( $post_id, $post, $update ) {
  if( $update == false ) {
    // Only assign ID to new bpark posts
    if ( $post->post_type == 'backend-park' ) {
 
      // get the most recent bpark post
      $bpark_args = array(
      'numberposts' => 1,
      'post_type' => 'backend-park',
      'orderby' => 'post_date',
      'order' => 'DESC'
      );
      $bparks = get_posts( $bpark_args );
 
      // get the project_id of the prior post
      $last_id = isset($bparks[0]) ? get_post_meta( $bparks[0]->ID, 'wpcf-bpark-id', true ) : 0;
 
      // increment
      $last_id++;
 
      // set the project_id of the current post
      update_post_meta( $post_id, 'wpcf-bpark-id', $last_id );
 
    }
  }
 
}
add_action( 'save_post', 'auto_assign_bpark_ids', 100, 3 );

However, the autonumber still updates on an edit. Which is really frustrating

#1809045

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hayley,

Here is the conditional that you can add.

if(empty(get_post_meta($post_id,'wpcf-bpark-id'))){

 // get the most recent bpark post
      $bpark_args = array(
      'numberposts' => 1,
      'post_type' => 'backend-park',
      'orderby' => 'post_date',
      'order' => 'DESC'
      );
      $bparks = get_posts( $bpark_args );
  
      // get the project_id of the prior post
      $last_id = isset($bparks[0]) ? get_post_meta( $bparks[0]->ID, 'wpcf-bpark-id', true ) : 0;
  
      // increment
      $last_id++;
  
      // set the project_id of the current post
      update_post_meta( $post_id, 'wpcf-bpark-id', $last_id );
  
}

This makes it so that the update_post only runs when the post meta is empty. If it already has a value the function will do nothing.

Thanks,
Shane

#1809327

As always, thanks Shane!

My issue is resolved now. Thank you!