I have a CPT for services. To all these service posts I’d like to add a large number of attributes (same on all posts, but with different values). Now I want a simple central place to handle the possible attributes, the attribute values on each post and the usage of attributes in template files.
Each attribute consists of a row of values/fields.
Here’s a pretty good visual view of the setup I’m thinking of (and I might actually use it to present a table like that): https://docs.google.com/spreadsheets/d/1FJTvWT5RHFSYuEoFVpAeQjuQPU4BVzbOigT0xebxTOw/htmlview?sle=true#gid=0
My initial thought was to handle the attributes per post like a repeater, since each attribute has a group of fields, not just a single text field. So, my hope was to have a separate repeater on an options page, to store available attributes and their value type and using those rows to populate the attribute repeater on each post (and on that repeater disable the possibility to change the label and value type field, and lock the row number), and just set the actual value and description.
I’ve seen some posts about populating repeater fields, but no complete solutions and I can’t really get it together from documentation and other posts.
Any help or possible solutions?
This is really 2 questions, I’m going to answer one at a time.
The first, how to pre load a repeater field on a new post from a repeater in the options field.
If the repeater and all of the sub fields are the same, so the two repeaters are identical, the it’s kinda easy, you just need to get the value from the options page and return in. What I did was I created a field group that contained my repeater and assigned it to both a CPT and an Options page, making them identical.
add_filter('acf/load_value/key=field_56e870552a8c1', 'load_repeater_value', 10, 3);
function load_repeater_value($value, $post_id, $field) {
if ($value !== NULL) {
// on a new post the value will === NULL
// if the value is not exactly NULL then bail
return $value;
}
$new_value = get_field('repeater', 'option', false);
// you can see this value if you uncomment the next line
//echo '<pre>'; print_r($new_value); echo '</pre>';
// if the repeaters are identical
$value = $new_value;
return $value;
}
On the other hand, If the repeaters are not identical and the repeater on the CPT has different field keys than the repeater on the Options page, or they are different is some other way then you have a lot of work to do. Comment out the line assigning the new value and uncomment the line that outputs the value gotten from the options page. What you’ll need to do is to build a new array using the correct field keys for the repeater located on the CPT.
The format of the array you need to return is
$value = array(
// a nested array for each row
array(
// key value pair for each sub field
'field_123456789' => $value,
// the next one is a nested repeater
'field_987654321' => array(
// a nested array for each row
array(
// key value pair for each sub_field
'field_784ab6767' => $vlaue
)
)
)
);
you can disable the disable editing of some fields by making them read only using and acf/load_value filter. When dealing with repeater sub fields you should always use the field key.
add_filter('acf/load_field/key=field_56e870552a8c1', 'make_field_readonly');
function make_field_readonly($field) {
// do not do this if it's the options page
// if you're using the same field group
global $post;
if ((!$post) || !isset($post->ID) || get_post_type($post->ID) != 'your-post-type') {
return $field;
}
$field['readonly'] = 1;
return $field;
}
The topic ‘Populate repeater in CPT from repeater in Options page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.