Support

Account

Home Forums General Issues Prepopulate repeater fields?

Solved

Prepopulate repeater fields?

  • I’d like to pre-populate repeater fields so my client doesn’t need to add them in every time, but can change them and add more as well.

    I’m not sure what code to use for this.

    Example:

    A repeater called Items, with two text fields that are already loaded for the client
    — color and shape — and the client fills in red and circle:

    Color: red
    Shape: circle

    And the client could add another row on their own, and reorder the rows.

    I found this code in another discussion but couldn’t figure it out:

    /**
     * Load Value in Details Repeater
     */
     //
    function my_acf_load_value($value, $post_id, $field) {
      if ($value !== NULL) {
        // if the value is exactly NULL it means
        // the field has never been updated
        // we don't want to change fields that have already been editied
        return $value;
      }
      // set the new field value
      $value = array(
        // add a nested array for each row
        array(
          // add an array item for each sub field
          'field_name_1' => 'Value for Field 1',
          'field_name_2' => 'Value for Field 2'
        )
      );
      return $value;
    }
  • First you need to add a hook to call your filter

    
    add_filter('acf/load_value/name=YOUR-REPEATER-FIELD-NAME', 'my_acf_load_value', 20, 3);
    

    In the function above you replace ‘field_name_1’ with the name of the first sub field and add the value for that field. You do this for each sub field. If you want to add a second row you add another nested array for that row’s values.

  • Whoops. I was missing that part — thank you.

    I got it to work as a repeater on posts, but when it’s a repeater inside a block it doesn’t. Maybe I need to add in the block key too? (The top key is the repeater and the key inside the array is the sub_field.)

    // Add field key of the repeater
    add_filter('acf/load_value/key=key=field_5cf9732cb05a', 'afc_load_my_repeater_value', 10, 3);
    
    function afc_load_my_repeater_value($value, $post_id, $field) {
    
    			 //Optional: Check for post_status otherwise published values will be changed.
    	 if ( get_post_status( $post_id ) === 'auto-draft' ) {
    
    			 $value	= array();
    
    			// Add field key for the field you would to put a default value
    			 $value[] = array(
    				 'field_5cf979117a655' => 'Label 1'
    			 );
    			 $value[] = array(
    				 'field_5cf979117a655' => 'Label 2'
    			 );
    			 $value[] = array(
    				 'field_5cf979117a655' => 'Label 3'
    			 );
    
    	 }
    	 return $value;
     }
  • Thank you @hube2! I wrote more above but forgot to tag you.

  • I think this part in the ACF block documentation is the problem… so I’m not sure if there’s a way to do what I want to do: “This prevents the $post_id parameter from working as expected in our template functions.”

  • I really can’t help you when it comes to blocks. But I do know that fields are associated with the block and not the post ID.

    This check in my original function tests to see if a value has already been set rather than depending on the post status

    
    //...
      if ($value !== NULL) {
        // if the value is exactly NULL it means
        // the field has never been updated
        // we don't want to change fields that have already been edited
        return $value;
      }
    
  • @hube2 And THAT solved it! Brilliant. Thank you so much.

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

The topic ‘Prepopulate repeater fields?’ is closed to new replies.