Check if an array of posts has posts from a specific category

I have an $array that prints out to be as follows.

Array ( [post-12] => 12 [post-160] => 160 )

What WordPress function could I use to check how many of the post ID’s in the array belong to a specific category, say category with ID 45.

I tried doing the following.

$post_id_array = $array;
$category = '45';
$count = count(in_category( $category, $post_id_array ));
echo $count;

It doesn’t work.

2 Answers
2

I have successfully returned the counts using the following code. Just in case someone may later view this question.

$count = 0;
$category = '45';
foreach ( $array as $post_id ) {
    if (in_category( $category, $post_id )) {
        $count++;
    }
}
echo $count;

Leave a Comment