2 comments

  1. I am not sure, but you could try something like this:

    $args = array(
        'posts_per_page'    => -1,
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'author'            => $current_user_id,
        'meta_key'          => 'color',
        'meta_value'        => array('red', 'blue')
    );
    
    $posts_query = new WP_Query($args);
    $the_count = $posts_query->post_count;
    
    echo $the_count;
    
  2. If you want to use the meta_query array, you have to put the meta_key and meta_value in a subarray:

    $args = array(
        'posts_per_page'    => -1,
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'author'            => $current_user_id,
        'meta_query'        => array(
            array(
               'key'        => 'color',
               'value'      => array('red', 'blue'),
            ),
        ),
    );
    $posts_array = get_posts( $args );
    $the_count = count($posts_array);
    

    This is because you can use multiple meta_key to combine them.

        'meta_query'        => array(
            'relation' => 'AND',
            array(
               'key'        => 'color',
               'value'      => array('red', 'blue'),
            ),
            array(
               'key'        => 'size',
               'value'      => array('l', 'xl', 'xxl'),
            ),
        ),
    

Comments are closed.