When you create a subfield of a repeater it’s given the name and key just like the parent field is given. so you create the filter for that field name or key.
http://www.advancedcustomfields.com/resources/filters/acfload_value/
For example, when I do this my setup will have 3 filters, on for the repeater and 1 for each of the 2 sub-fields in that repeater. The first filter will set a number of default rows for the repeater based on the number of rows in options. The other two will set the values for each row. The only thing I left out of my description above is that I only want to set the value if the first filter actually set the number of rows. So my first function would actually look something like:
add_filter('acf/load_value/name=my_repeater', 'afc_load_my_repeater_value', 10, 3);
function afc_load_my_repeater_value($value, $post_id, $field) {
if ($value === false) {
// this is a new post since value === false
// set value to number of default # of rows to create
// based on number of rows created on options page
$value = 5;
// add filters to call subfield filters here
// they are only created if we're supposed to use the default values
add_filter('acf/load_value/name=sub_field_1', 'afc_load_sub_field_1_value', 10);
add_filter('acf/load_value/name=sub_field_2', 'afc_load_sub_field_2_value', 10);
}
return $value;
}
function afc_load_sub_field_1_value($value) {
// get next value for subfield 1 and return
return $value
}
afc_load_sub_field_2_value($value) {
// get next value for subfield 2 and return
return $value
}
the actual subfield filters would be a bit more complicated because the values from the options page would need to be stored in an array in a global variable so that it can be accessed by my function and then each function would need to step through the values to return the next value for each subfield.
Like I said, I have not actually gotten to building the part where the subfields are set to the options page value. I just know what I’ll need to do when I get back to it and will test and figure out the details then.
Sorry, that’s about the best I’m going to be able to do as far as an explanation right now.
I’m building a plugin, so I’m building everything into a PHP class. I say this because you need to create some counters to keep track of where you are. If you’re not using classes then you’ll need to use global variables to do the counters.
Here are the steps.
During initialization I read in the values from the repeaters and store them into an array. The array will need to be either a class property or global variable. this function should probably run at “plugins_loaded” to make sure that ACF functions are available for retrieving your repeater field data.
the next step is to set the correct number of repeats. That’s done with the function I gave in my last comment. You just need to return the count of the array that holds your subfield data.
For each subfield you need to create a another filter, along with a counter to keep track of where you need to be.
global $subfield_1_counter;
$subfield_1_counter = 0;
add_filter('acf/load_value/name=subfield_1', 'afc_load_dubfield_1_value', 10, 3);
function afc_load_dubfield_1_value($value, $post_id, $field) {
global $subfield_1_counter;
// code to calculate next value for this field
// the exact code here will depend on the type of field being initialized
// after setting the value, increment the counter and return the value
$subfield_1_counter++;
return $value;
}
To be honest, I don’t have exact details on what I did because I haven’t completed this part yet. Like I said, I’m doing this for a plugin that I’m building and my attention has been diverted from that project. This is how I plan to proceed when my workload drops enough so I can return to it.
Hope this helps.
There’s definitely interest, I’m working on a plugin that will allwo HTML, CSS an JS to be entered. Mixed code would be something I’d like to see. As far as PHP goes though, I don’t let people do PHP through the admin, so not needed.
Didn’t even know it existed, I’ll give it a try.
There do seem to be some limitations that’ll effect it’s use and from giving it a quick look it doesn’t seem to be supported at all.
Just in case anyone else needs this information.
So, to create an arbitrary number of subfields for a new post, if I have a repeater named “bf_test_repeater”
add_filter('acf/load_value/name=bf_test_repeater', 'afc_load_bf_test_repeater_value', 10, 3);
function afc_load_bf_test_repeater_value($value, $post_id, $field) {
if ($value === false) {
// this is a new post since value === false
// set value to number of default # of rows to create
$value = 5;
}
return $value;
}
I’ll have more code than this since it will need to figure out by looking at the options page how many rows to start with. Then I’ll need the code for each of the sub-fields to set the value to the next value retrieved from the options page.
I’m guessing that my problem was/is that there are no subfields to begin with in what I was trying.
I just did this on another repeater that has a minimum number of subfields that must be set and it works fine.
So what I guess I really need to figure out is how to dynamically create some arbitrary number of subfield rows when a new post is being created.
I’ve tried using acf/load_value, but I’m missing something when it comes to the subfields of a repeater. Heh, I’ve figured out how to populate default content in in almost every other type of field but this one has me stumped for some reason ๐
Do I add the filter for the repeater field or each of the subfields?
I guess I need to wait, no export to PHP yet. There is a bug in the export as it is, I’ll post that to issues on github
I’m working on creating a duplicate of the R&D site I am working on the new plugin on. I’ll try install/update to ACF5 there and see how the export works, among other things.
I’ve been trying to watch the progress on ACF5, there were a few missing features the last time I looked that I really need to have to work on this plugin.
Heh, and all my nice coding was altered here as well, see if this works I enter & lt ;
(remove the spaces) and it is exported as <
In the above code
'capability_type' => 'page'
is what caused the problem, if I change that to post, then there isn’t a problem.
For now I’m using ‘post’, It isn’t a huge difference in this case. Really just wanted people to be aware of it.
register_post_type($this->slug,
array('label' => 'Supplemental Content',
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'capability_type' => 'page',
'hierarchical' => false,
'rewrite' => false,
'query_var' => true,
'exclude_from_search' => true,
'menu_position' => $this->options['post_type']['menu_position'],
'supports' => $this->options['post_type']['supports'],
'labels' => array ('name' => 'Supplimental Content',
'singular_name' => 'Page',
'menu_name' => 'Supplimental Content',
'add_new' => 'Add Page',
'add_new_item' => 'Add New Page',
'edit' => 'Edit',
'edit_item' => 'Edit Page',
'new_item' => 'New Page',
'view' => NULL,
'view_item' => NULL,
'search_items' => 'Search Pages',
'not_found' => 'No Pages Found',
'not_found_in_trash' => 'No Pages Found in Trash'),
'description' => 'This post type has been added by the plugin
"Supplimental Content". It allows the user to create
content that can be added to archive pages on the site.
For more information read the documentation for the plugin.'));
As you can see in this first image, when the capability_type of the custom post type is “post” you can select a specific post to add the field group to.
In these next two the capability_type of the custom post has been changed to page page then a specific post cannot be selected, unless I’m still missing something.
Figured this out for anyone that has the same problem.
When creating a custom post type, if you want to be able to choose specific posts in the post type then the value of the argument ‘capability_type’ must be ‘post’
Not sure if this was intended or not.
Cool!
Great, thanks for the reply. I’ll probably stick with slightly altering whatever was created as the key when the field was created. Easy enough to use a preg_match to grab the fields and then duplicate and alter.
Actually, I believe what you want to do may be possible. I have run into a similar need.
In my case I have a custom post type and in that post the user can set the item as either active or inactive. When they select inactive they are then shown a select box where they can select an alternate item to use in its place. I could have used the Post Object or Relationship type field for this, however, this would allow them to select another item that was also inactive. I did not want to do this because it would just confuse them and make them do the work of selecting an active item.
Then I found this tutorial: Dynamically populate a select fieldโs choices
http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/
(sorry for the long link, for some reason the editor would not allow me to add the link the right way.)
Anyway, the tutorial shows you how to use an options field to populate a select box. However, with a little work I was able to change it to a query to get only the items that are marked as active and built my own select that way. I have found this useful several times with the current project I’m working on.
You might be able do the something similar but in your case you can create a query that gets a list of the projects and then a loop to pull the list of images in each project. Then your user could select from that. Unfortunately there might be more work because you would only be able to add text to the select field to choose from and I don’t think you can show the actual images in the select field so it may not be a perfect workaround.
Worked a charm, added your code to my custom plugin class for this site. I have not tested it completely, only tested to see if it worked on the parent level so far. Will test the rest as I use it. But after reading the instructions that Elliot linked to and looking at your code I don’t see why it wouldn’t work.
Thanks a heap, saved me a boatload of time trying to figure it out on my own. Now I have an example to follow ๐
Thanks, I’ll give it a whirl and let you know how it goes.
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.