Support

Account

Home Forums General Issues Create a number field with Auto increment Value

Solving

Create a number field with Auto increment Value

  • Hi,
    I have “Invoice” post type, I created some custom fields in this invoice. when I add records, the WordPress’s ID is not increase sequentially. its increasing like:
    example.com/invoice/55/
    example.com/invoice/57/
    example.com/invoice/59/
    example.com/invoice/61/

    I need to make it sequentially like 55,56,57,58.. but it seems impossible because of WordPress’s features.

    For that reason I want to create another custom field with ACF like “inv_ID” and let this fields value add a number sequentially.

    I hope my request is clear. It’s really important matter to me I hope you could help me in this matter. Thanks!

  • In order to do this you would need to create another field or use a standard WP option value. You will need something to keep track of the last value used.

    Then you could create a new field and using acf/prepare_field make the field readonly and get the value of the option to set the value of the field. Then increment and update the option.

  • Actually I only need one custom field which it increment sequentially. The links could stay like that:
    example.com/invoice/55/
    example.com/invoice/57/
    example.com/invoice/59/
    example.com/invoice/61/

    Can you please guide me how can I do this?
    Thank you in advance!

  • there isn’t a way to create an auto-incrementing field in ACF.

    The closes you could come would be similar to the approach I already mentioned but instead of storing the last number used to do a query on existing posts to find the post with the highest number.

    Of the choices, using an option value is the easiest approach and the one that is least likely to run into issues.

  • Thank you for your response.
    This method worked to me:

    add_filter( 'wp_insert_post_data' , 'odin_prj_title' , 10, 2 );
    function odin_prj_title( $data , $postarr )
    {
        if( $data['post_type'] == 'prj' && $data['post_status'] != 'publish' ) {
            $count_posts = wp_count_posts('prj');
            $published_posts = $count_posts->publish;
            $data['post_title'] = 'P0' . ($published_posts + 1);
        }
        return $data;
    }

    https://wordpress.stackexchange.com/posts/296509/revisions

  • There is an issue with the code above.
    It created sequential numbers by counting posts in specific post type. But when I remove an entry of this post type, the numbering is starting to make similar numbers which is mess up the thing.

    Can you please suggest me a way or idea to prevent this confliction?

    Thank you!

  • This is why I suggested the approach that I did originally. You cannot rely on anything associated with a post because that post can be deleted. The only way to do this without errors is to have some consistent value that cannot be removed.

  • Why not create a custom post type called control with the number stored in it and remove it from the dashboard. The chances of it being deleted is almost nil.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.