Avatar of ClintonK
ClintonK
Flag 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
HTMLPHPWordPress

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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"
Julian Hansen

Have you tried this
$value = get_field( "section" );

Open in new window

ClintonK

ASKER
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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Julian Hansen

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.
ClintonK

ASKER
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
Julian Hansen

You  are welcome.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.