Looking for a way to get the list of Checkbox choices for an ACF field.
So, if I have an ACF field myChoices with choices
1 : Small
2 : Medium
3 : Large
In my page template I want to display a list (for filtering purposes) of all the choices.
I was previously doing this
Saved to $fieldkey
$field = get_field_object($fieldkey);
I could then access the choices from
$choices = $field['choices'];
This seems to have stopped working after the last ACF update (5.7.7)
I don’t think my approach was standard – but it worked.
Any ideas how to do this in a more standards compliant way?
@julianmarcorpsa-com Have you tried dropping the underscore from the meta_key value…so just
= 'mychoices'. I ask because when I use a plugin called Search & Filter Pro, I have to select the meta key to pick which which field I want to filter by. In the list of available fields, each field shows up twice, one with a preceding underscore and one without. I have to use the one without in order for it to work correctly.
But if that’s not the case, you should be able to do something like:
<?php $values = get_field('mychoices'); $field = get_field_object('mychoices'); $choices = $field['choices']; ?> <ul> <?php foreach( $choices as $choice => $label ) : ?> <li>Value: <?php echo $choice; ?>, Label: <?php echo $label; ?></li> <?php endforeach; ?> </ul>
Let me know if that doesn’t work for you. I didn’t get a chance to test out, but I just did something similar the other day.
Thanks for the response.
Yes I did – no joy.
When I did some more digging I found there was more than one key value in post_meta for that field – I have noticed that under some conditions ACF does not clean up after itself and you end up with Ghost entries.
Either way I eventually resolved to create another ACF field – in its own group. We setup the choices there and bind the field to the page on which we need those values. In the page properties we then just select all the choices available and save to the post – that way we can just get_field() them.
I would have preferred that we could just get the values from the field meta data – but the pressure to get this working dictated we find another solution. The second ACF is a bit cumbersome but it works and removes the requirement to query the post_meta table directly.
Thanks again for the response.
You must be logged in to reply to this topic.
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!
🎥 Hi ACF peeps, I’m thinking about recording some raw/unedited “real world” examples for using ACF Blocks.— Advanced Custom Fields (@wp_acf) May 26, 2019
My first idea is a “Slider” block using a JS carousel library.
What else would you like to see?
© 2019 Advanced Custom Fields. Subscribe