ClintonK
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Have you tried this
$value = get_field( "section" );
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;
}
}
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.
The get_field("section") will try and find the ACF field "section" value for the current post.
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
This code does the job.
Thanks
You are welcome.
ASKER
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"