Support

Account

Home Forums ACF PRO Pre-populated incremented reference number field

Solving

Pre-populated incremented reference number field

  • Hi there
    I created a real estate website and used ACF to create the fields for adding new properties. The one thing I couldn’t figure out was how to have a field that will automatically increment the reference number by one every time a new property is added (and pre-populate the field).
    I am more of an HTML/CSS person so if this needs back-end coding I’m keen to learn, but please help me step by step.
    This is the website in question: https://www.stanfordvillageproperties.com
    Currently we are adding the ref number manually which is not ideal.

    I will appreciate any help on this
    Thanks in advance
    Marisa

  • What you need to do is create an WP option that holds the value last used.

    
    // on WP init create an option value if it does not exist
    // this code can be deleted after the option is created
    // or it can be left alone
    add_action('init', 'create_my_reference_options');
    function create_my_reference_options() {
      $reference = get_option('reference_field_counter', false);
      if (!$reference) {
        update_options('reference_field_counter', 12345);
      }
    }
    

    Having a counter in place you can then use an acf/prepare_field filter to set the value if it is not set

    
    add_filter('acf/prepare_field/name=reference_field_name', 'set_reference_value');
    function set_reference_value($field) {
      if (empty($field('value'])) {
        $reference = intval(get_option('reference_field_counter'));
        $reference++;
        $field['value'] = $refefence;
        update_options('reference_field_counter', $reference);
      }
      return $field;
    }
    

    Note that this will continue to increment the reference number each time the page is loaded even if the post is not saved and may leave gaps in your numbering system. If this in not the case then it gets more complicated. Rather than using an option you would instead need to do a query in the prepare_field filter to get the post with the highest number and then you that value to set the new one. But this also has drawbacks. In the instance where multiple posts are created at the same time then it is likely that numbers will be repeated. I doubt there is a full-proof method.

  • This reply has been marked as private.
  • Typo on this line

    
    if (empty($field('value'])) {
    

    should be

    
    if (empty($field['value'])) {
    

    Can’t see typos easily here when adding code. There are other typos as well, I did not always spell references correctly. Sorry about these, I was just typing out a quick example and sometimes it happens.

    All of the code should go in functions.php. Automating things can sometimes be difficult. You have to way the time it takes to do the automation vs how much time it will save.

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

You must be logged in to reply to this topic.