I’m trying to show the number of posts that have certain meta key
values
for the current user.
This is my code:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
'key' => 'color',
'value' => array('red', 'blue')
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
echo $the_count;
Thi is counting ALL posts for the current user, ignoring the meta key
values
.
I only need the $the_count
to be the number of posts that have a meta key
value
'red'
or 'blue'
for the current user.
What am I doing wrong?
Thanks in advance!
I am not sure, but you could try something like this:
If you want to use the
meta_query
array, you have to put themeta_key
andmeta_value
in a subarray:This is because you can use multiple meta_key to combine them.