php - Count posts by values from Advanced Custom Field (wordpress)


I'm trying to count the posts that have the same values checked in a checkbox from the ACF module.

I have a code working for radio buttons, but it does not work on a checkbox where multiple choices are available:

My code so far:

    function get_post_count_by_meta( $meta_key, $meta_value, $post_type) {

    $args = array(
            'post_type' => $post_type,
            'numberposts'   => -1,          
            'post_status'   => 'publish',

        if ( $meta_key && $meta_value ) {
                if ( is_array($meta_value) ) {
            $args['meta_query'][] = array(
                'key' => $meta_key,
                'value' => $meta_value, 
                'compare' => 'LIKE');

        else {
            $args['meta_query'][] = array('key' => $meta_key, 'value' => $meta_value);

        $posts = get_posts($args);

        $count = count($posts);

    return $count; 

 $post_count = get_post_count_by_meta('test_field', 'Value 1', 'any');
echo $post_count;

This always responses 0 when the field is a checkbox. There must be something wrong with the $args query. Can someone give me a hint? Thanks




Thenumberposts parameter should beposts_per_page (see docs)

People are also looking for solutions to the problem: png - access a gd image resource in php as a matrix


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.