tags:

views:

113

answers:

1

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.

+1  A: 

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;
Richard M
+1, no sense in answering this, you just did :)
Tim Post
Richard, this works great, but one question. Since we are trying to find the total number of posts, why are you setting the posts_per_page argument? Is the assumption, since we will show a "view all" if the $NumberOfPostsToShow is less than the total number of posts, then we just need to know if post count is >= $NumberOfPostsToShow?
Scott B
@Richard, when I set $NumberOfPostsToShow to -1 (which is usually the default for "show all"), I'm getting 0 for $myrecentpostscount. What do I need to specify for "posts_per_page" in order to pull a count of all posts?
Scott B
Update: I also just found the wp_count_posts() function (reference: http://codex.wordpress.org/Function_Reference/wp_count_posts) but it does not appear to support arguments such as category exclusions.
Scott B
You should specify the number of posts you want to display as the `posts_per_page` argument, the `found_posts` property of the query object will contain the total number of posts for that query regardless of pagination or what you have set as `posts_per_page`.
Richard M
Will it affect performance if I simply leave posts_per_page off in the query above? I'm unable to get it to work if posts_per_page is unlimited (ie, -1) it returns 0 for post count.
Scott B
If you omit `posts_per_page` it will use the value you have set on the Wordpress reading settings page. I don't understand the problem, in the code in your question you seem to be getting a subset of posts matching some conditions using `'showposts' => $NumberOfPostsToShow` and then getting all the posts that match the conditions in order to count them. Using `WP_Query` as in my answer you can get an object containing the subset of posts and the count of all posts matching your conditions in one step.
Richard M
I did a test and set Settings > Reading > "Blog pages show at most" to 3, however, when I echo $myrecentpostscount it equals the total number of posts meeting the criteria (5) which is obviously greater than the 3 I set in reading settings. Something is amiss.
Scott B
@Richard, all I'm trying to do with the count is determine whether to show a "View All" link beneath the post listing. Example: if the user has selected "5" as the maximum number of posts to display, but they have 6 posts matching the criteria, I will draw a "View All" link. However, if they have selected "6" as the number of posts to display, but they only have 5 matching the criteria, then I don't show the "View All" link, since they are currently seeing all the posts anyway. That's the sole reason for the count. Perhaps you can suggest a better method? I'm sure it could be made better.
Scott B
Use the code in my answer, set `$NumberOfPostsToShow` to the number of post selected by the user. Then if the `max_num_pages` property of the query object is greater than 1 you should display your "View all" link. Or see the answer I gave when you asked this question previously: http://stackoverflow.com/questions/2134022/wordpress-get-count-of-posts-without-using-get-posts/2134294#2134294
Richard M