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
Answer
Solution:
The
numberposts
parameter should beposts_per_page
(see docs)