Ryan Achten
Ryan Achten

Reputation: 525

Accessing all Advanced Custom Field checkbox values

Is it possible to access all values for an ACF Checkbox field available in the ‘Choices’ field settings?

enter image description here

These values are being used to populate a filter dropdown, so therefore simply associating these values with a singular $post leads to inconsistent results. i.e. my current approach only works when there are posts associated with the custom field to populate the dropdown:

$courses_fieldObj = get_field_object('course_check');

$courses = $courses_fieldObj['choices'];
foreach ($courses as $value => $label): ?>

The ‘options’ argument looked promising, however, this doesn’t seem to make available the ‘Choices’ values.

Solutions outside of ACF’s api (such as WPDB queries suggested here) also look promising, however, since ACF stores these vals in a serialised array, I am hoping for a simpler solution.

Many thanks in advance!

Upvotes: 2

Views: 1107

Answers (1)

Ryan Achten
Ryan Achten

Reputation: 525

Was not able to solve this issue. I did, however, work around it by instead creating a custom taxonomy (which really suited my use case better), allowing me easy access to all course terms via the get_terms function, i.e:

$args = array( 'taxonomy' => 'courses_tax' );
$courses = get_terms( $args );

I can still see scenarios where the need to access all ACF checkbox values outside of a $post context will be required - will leave this question unsolved as this answer doesn’t directly address the issue at hand.

Upvotes: 1

Related Questions