按高级自定义字段(wordpress)中的值计数帖子

时间:2013-07-19 08:34:26

标签: php wordpress count wordpress-plugin posts

我正在尝试从ACF模块的复选框中查看具有相同值的帖子。

我有一个代码用于单选按钮,但它不适用于有多个选项的复选框:

到目前为止我的代码:

    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;

当字段为复选框时,此值始终为0。 $ args查询一定有问题。有人能给我一个暗示吗?感谢

1 个答案:

答案 0 :(得分:0)

numberposts参数应为posts_per_pagesee docs