Created
January 12, 2022 08:15
-
-
Save blogjunkie/b55918de943ba5aa8850bea8b16f9c1e to your computer and use it in GitHub Desktop.
Pre-fill ACF repeater sub-fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Let's say we have an ACF repeater field with 2 sub-fields: | |
* | |
* Section | |
* - Title | |
* - Description | |
* | |
* This code snippet will automatically load 3 instances of the Section, and pre-fill the titles. | |
*/ | |
function my_acf_set_repeater( $value, $post_id, $field ){ | |
// Debug | |
// var_dump($value); | |
// If value is not FALSE, it means the repeater has already been edited | |
// before so we return the $value and exit | |
if ($value !== FALSE) { | |
return $value; | |
} | |
// field_61de7bd327949 is the key of the Title sub-field we want to set | |
// Define the $value array with 3 sub-arrays to set the Titles | |
$value = array( | |
array( | |
'field_61de7bd327949' => 'Section Title One', | |
), | |
array( | |
'field_61de7bd327949' => 'The Next Section', | |
), | |
array( | |
'field_61de7bd327949' => 'Another Section Title', | |
) | |
); | |
return $value; | |
} | |
// acf/load_value/name={$field_name} - filter for a specific value load based on it's field name | |
add_filter('acf/load_value/name=section', 'my_acf_set_repeater', 1, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment