Home Forums General Issues Need help for auto-increment field with update_field


Need help for auto-increment field with update_field

  • Hello,

    For a sports licensing system I would like to have a “number” field that auto-increments each new creation of the custom post.

    I think I should use something like update_field but it would really help me to get help 🙂

    thank you in advance

  • Maybe a little help ?
    Thank you in advance!

  • There really isn’t a way to do this simply.

    There are a couple of choices.

    The is to create an acf/save_post filter In this filter get the value of the field and save it as an option Then create an acf/load_field filter and set the default value for the field based on the option.

    Second choice. Create an acf/load_field filter. In this filter you do a query to get the posts of the post type. Order DESC by the custom field, limit the posts returned to only get 1 post. Get the value of the field on this one post and set the default value of the field based on this value.

    Either has it’s pros and cons

  • Thank you very much for your leads, I found a solution that seems to work well :

    function ebo_renseigne_num_affiliation($value, $post_id, $field) {
    	if (! $value && get_post_type($post_id)=='club_type') {
    		$last_num = (int) get_option('ebo_dernier_num_club', '3000');
    		update_option('ebo_dernier_num_club', $last_num);
    		$value = $last_num;
    	return $value;
    add_filter('acf/update_value/name=numero_affiliation', 'ebo_renseigne_num_affiliation', 10, 3);

    I also wanted to make the field uneditable with this function:

    function msk_acf_disable_field($field) {
        $field['disabled'] = true;    
        return $field;
    add_filter('acf/load_field/name=numero_affiliation', 'msk_acf_disable_field');

    But that does not seem to be compatible.

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

The topic ‘Need help for auto-increment field with update_field’ is closed to new replies.