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');
$catHidd开发者_如何转开发en=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.
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;
精彩评论