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
ClintonKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Refer ACF man pages
https://www.advancedcustomfields.com/resources/get_field/
https://www.advancedcustomfields.com/resources/code-examples/#query-posts%20with%20acf%20values
Adapted from samples from above merged with your code
<?php
// Get the matching posts
$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( "youtube" );

    // DO SOMETHING USEFUL WITH $value HERE
  }
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ClintonKAuthor Commented:
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"
0
Julian HansenCommented:
Have you tried this
$value = get_field( "section" );

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ClintonKAuthor Commented:
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

0
Julian HansenCommented:
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.
0
ClintonKAuthor Commented:
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
0
Julian HansenCommented:
You  are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.