Link to home
Start Free TrialLog in
Avatar of ClintonK
ClintonKFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to get the value of an ACF Checkbox?

I've got a Custom Post called you "youtube" which has three ACF custom fields, one of which is a Checkbox (color)  that can have three values;

red : Red
green : Green
blue : Blue

I've created a post array using this code:
      $youtube_ids = get_posts(
      array(
            'post_type'      => 'youtube',
            'post_status'    => 'publish',
            'posts_per_page' => - 1,
            'fields'         => 'ids',
      ) );

For each post I want to see if a certain checkbox value has been set.
Something along the lines of this pseudo code:

foreach youtube_ids value
 if Color == red
    do some html stuff
endif
endforeach
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ClintonK

ASKER

I think I'm missing something.
I've revisited this and my youtube post now contains the three fields:
registration (text)
tubeid (text)
section (checkbox with 4 values)

This is as far as I've got with the code and this returns the tubeid
<?php      
$youtube = get_posts(
      array(
      'post_type'      => 'youtube',
      'post_status'    => 'publish',
      'posts_per_page' => - 1,
      'fields'         => 'ids',
      ));

// IF SUCCESSFUL ...
if($youtube)
{
  // ITERATE OVER THE POSTS
      foreach($youtube as $post) {
            $value = get_field('tubeid', $post);
            echo $value . '<br>';
      }
}
?>

I'm afraid I still cant see how to get the checkbox values for "section"
Have you tried this
$value = get_field( "section" );

Open in new window

like this?
 $youtube = get_posts(
	array(
	'post_type'      => 'youtube',
	'post_status'    => 'publish',
	'posts_per_page' => - 1,
	'fields'         => 'ids',
	));

// IF SUCCESSFUL ...
if($youtube)
{
  // ITERATE OVER THE POSTS
	foreach($youtube as $post) {
		$value = get_field("section");
		echo $value;
	}
}

Open in new window

Based on the description of your setup then yes - like that.

The get_field("section") will try and find the ACF field "section" value for the current post.
This was the successful. Subsequent issues were due to my grasp of the solution and issues with my data.
This code does the job.
Thanks
You  are welcome.