Support

Account

Home Forums ACF PRO Check if any choices is selected of a checkbox

Solved

Check if any choices is selected of a checkbox

    • mnpdev

    • April 7, 2021 at 8:03 pm

    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
    }
    
    • mnpdev

    • April 8, 2021 at 12:55 am

    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.

    • mnpdev

    • April 8, 2021 at 5:17 pm

    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.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.