Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Dynamic Checkboxes based on other custom fields
-
Hoping someone can help!
I've got a post type (specifically a product) which needs to allow for checkbox or radio box selection.
And i've created a repeater field which has a sub field in it allowing for colour selection.
Now on the post type page i want a user to be able to select one of those options which are dynamically generated based on the sub field. Is this possible?
Thanks,
Adam -
-
So essentially i've created one custom field set, which contains a repeater field on an options page.
I want to be able to make it so that within the admin section (on the editor page of the post type it's assign to) that you can then select which of those fields yours matches.
So say if i created:
Colours - Repeater
- Name of colour
- Colour picked
Basically on a post type page (post, or page or 'product in this case') that you get a group of checkboxes or radioboxes where i can select from the name of the colour (ie: red, green or black).
I hope that makes more sense! -
Basically generate a series of checkboxes or radioboxes based on repeater field.
-
Hi @crashfellow
Thanks for clarifying. Yes this is very possible and not too hard at all.
Just make a select field but leave the choices empty.
Then use the acf_load_field hook to dynamically generate the options.
You can read how to use this hook here: http://www.advancedcustomfields.com/docs/filters/acf_load_field/
Inside you hook, you can lookup the repeater field value from the options page and then customize the $field['options']
Good luck!
-
I'm going to try this now! here's hoping it works lol
Once this connection is done, how do you grab that data on the actual page? or is it smart enough to make that connection between the dropdown/checkboxes and the data in the original repeater? -
I've worked out how this is working, thanks for that.
I will say though, it doesn't seem to accept the values, and i even used your default code and used the key of the empty select field i made. -
It works on that field and all fields if i just use : acf_load_field
But as soon as i use the field key it no longer works.
I looked at the field of the select field which is : field_50bea689a6921
And i've put:
add_filter('acf_load_field-field_50bea689a6921', 'my_acf_load_field');
But it doesn't seem to work... which is weird because i mentioned, it works if you do it on all fields. -
I'm having the same problem.
add_filter('acf_load_field', 'my_acf_load_field');
applies the filter butadd_filter('acf_load_field-field_50be1438982eb', 'my_acf_load_field');
doesn't.
Is this a bug?
My workaround for now is to check for the fieldname in the all fields function...function my_acf_load_field( $field ) {
if($field['key'] == 'field_50be1438982eb') {
$field['choices'] = array(
'custom' => 'My Custom Choice',
'custom1' => 'My Custom Choice 1'
);
}
return $field;
} -
Yeah same problem.. gonna try your work around! thanks man, much appreciated.
-
yep does work! kind of embrassed i didn't think of it lol but ohh well. But yeah, there's definitely a bug there.
-
Hi @crashfellow,
I'm not sure why the field key is not working for the hook. Looking ta the code on acf.php: line 610, I can see that ACF should be calling that action...
I'll do some testing and let you know what I find
Cheers
Elliot -
Hi @crashfellow, @launch
I just tested the hook and can confirm that it does work for the field key.
The only way for the hook 'acf_load_field-$field_key' not to fire is if the $field does not have a 'key'. Can you check your database and look for the value of 'field_50be1438982eb' in the wp_postmeta table?
Also, if its of any use, I'm running the latest ACF version which saves the fields as 'field_19'
Cheers
Elliot -
I've got the same issue on a different site... This is the data in my post_meta table:
a:12:{s:7:"choices";a:0:{}s:5:"label";s:13:"Category Name";s:4:"name";s:13:"category_name";s:4:"type";s:6:"select";s:12:"instructions";s:0:"";s:8:"required";s:1:"0";s:13:"default_value";s:0:"";s:10:"allow_null";s:1:"0";s:8:"multiple";s:1:"0";s:17:"conditional_logic";a:3:{s:6:"status";s:1:"0";s:5:"rules";a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}}s:8:"allorany";s:3:"all";}s:8:"order_no";i:0;s:3:"key";s:7:"field_6";}
I've tried acf_load_field-field_6 & acf_load_field-category_name as the and they both don't work. Am I missing something? -
-
I just upgraded to the latest version of ACF and its now working!.. Here is my code..
function acf_load_categories( $field ) {
$categories = get_categories();
$cats = array(''=>'');
foreach($categories as $cat) {
$cats[$cat->cat_ID] = $cat->name;
}
$field['choices'] = $cats;
return $field;
}
add_filter('acf_load_field-field_6', 'acf_load_categories');
-
I find that the acf_load_field filter works OK, but the acf_load_field- filters do not. I traced the problem down to acf.php in the get_acf_field function. Line 621 in version 3.5.7.1. The apply filters return to a variable "$value" but should return to a variable "$field." $value is never used again in the function.
$value = apply_filters('acf_load_field-' . $field[ $key ], $field);
//Should be
$field = apply_filters('acf_load_field-' . $field[ $key ], $field);