Skip Navigation

[Resolved] Number will increment when editing post “Auto Numbering Posts” vs2

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically increment a custom field value when I create a new post in my custom post type. The field value should be the highest field value from the other posts, plus one.

Solution:
Add this code to functions.php:

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

Relevant Documentation:
https://codex.wordpress.org/Template_Tags/get_posts
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://codex.wordpress.org/Function_Reference/update_post_meta

This support ticket is created 6 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by hpJ 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#644074

Christian,

Please read about my problem in this post: https://toolset.com/forums/topic/number-will-increment-when-editing-post-auto-numbering-posts/

I could not get it resolved there and decided to open a new ticket as I was not getting anywhere with that supporter. His first answer is to the point but for some reason that did not solve the problem. From there on it became a disaster as if he was not even reading his own answers anymore.

I hope you can solve this.

#644273

Hi, I think the problem is that the post_status isn't publish on the first auto save, so the conditional here is blocking the rest of the code:

if ( $post->post_status == 'publish' && $post->post_type == 'telorder' ) {

Change it to this:

if ( $post->post_type == 'telorder' ) {

Let me know if this does not resolve the issue and we can take a closer look.

#644495

No, did not work. The first new entry gave a blank 'ordernummer', the second one gave 1 and the third one gave als 1.

Editing the order did not increment the number, so that's already good.

Tell me if you need logins for admin and ftp.

#644615

Okay I tested this out and it seems to be working more consistently now:

/**
* Add an auto-incrementing ordernummer field to telorder posts
*/
function auto_assign_ids( $post_id, $post, $update ) {
  if( $update == false ) {
    // Only assign ID to new telorder posts
    if ( $post->post_type == 'telorder' ) {

      // get the most recent telorder post
      $telorder_args = array(
      'numberposts' => 1,
      'post_type' => 'telorder',
      'orderby' => 'post_date',
      'order' => 'DESC'
      );
      $telorders = get_posts( $telorder_args );

      // get the project_id of the prior post
      $last_id = isset($telorders[0]) ? get_post_meta( $telorders[0]->ID, 'wpcf-ordernummer', true ) : 0;

      // increment
      $last_id++;

      // set the project_id of the current post
      update_post_meta( $post_id, 'wpcf-ordernummer', $last_id );

    }
  }

}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );

I modified the logic for determining the most recent post. Please create at least one new post and manually set the ordernummer value to be some number greater than the other ordernummers. Then when you create the next new post, it should be automatically set correctly. Future updates will not modify the ordernummer field.

#644621

Hi Christian,

It seems to be working fine. The number increments with new orders and stays the same after an edit. I will test it some more, but I believe you solved the problem.

Very nice!

Regards,
Jos

#1080172

hpJ

This works perfectly! Thank you.