Hello,
Is there a way to check if a choice of a checkbox field is checked in any post?
You have to do a WP_Query to get posts using a meta query, if you are just looking to see if any if it is checked anywhere then you could limit the query to return one ID
$args = array(
'post_type' => 'your post type',
'post_status' => 'pubish',
'posts_per_page' => 1,
'fields' => 'ids',
'no_found_rows' => true,
'meta_query' => array(
array(
'key' => 'checkbox field name',
'value' => '"'.$field_value_to_find.'"',
'compare' => 'LIKE'
)
),
)
$my_query = new WP_Qeury($args);
if (!empty($my_query->posts)) {
// there is a post with this value checked
}
Hello,
Thanks for replying. With this I get all posts with a option selected. But I am looking for a way to make a list of the selected options of a checkbox field.
for example:
checkbox field has the options: red, blue and green
post1 has red and post2 has green
And then I get a list with the options red and green
Something like :
$terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' => true, ) );
But than for a checkbox field.
You are talking about doing a search type application where only existing values are shown.
To do this you would need to do a query to get all of the posts, loop over them and see what is set. This would be a time consuming process.
The other option is to keep track of what is available.
But there isn’t any way to find out which values are selected directly.
Unless you’re saying that the checkbox field is a taxonomy field and then that would be a completely different story. But if this is the case it is not obvious from your description.
There going to be over 5000 posts. so the query solution won’t be very good solution.
Well you gave me an idea for transforming it to a taxonomy field. I will try that. And see if that works.
Thanks for the help!
Yes, this is what taxonomies are for and they make it easy to get a list of terms that have posts.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.