Scott B
Scott B

Reputation: 40157

WordPress get_post_count?

I'd like to create a function that retrieves the post count for a given query. I don't want to use get_posts obviously as its way to expensive for this purpose. However, that's exactly what I'm having to use in absense of a get_post_count function.

My code is...

global $post;
$cat=get_cat_ID('mymenu');
$catHidden=get_cat_ID('hidden');
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => $NumberOfPostsToShow));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => -1));
$myrecentpostscount = count($myrecentposts2);

Note: get_posts() is a core WP function.

Upvotes: 1

Views: 3747

Answers (1)

Richard M
Richard M

Reputation: 14535

You could create a new WP_Query object, the found_posts property of which would be your post count.

$myquery = new WP_Query();
$myquery->query(array(
    'cat' => "-$cat,-$catHidden",
    'post_not_in' => get_option('sticky_posts'),
    'posts_per_page' => $NumberOfPostsToShow
));

$myrecentpostscount = $myquery->found_posts;

Upvotes: 4

Related Questions