Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have a wordpress site that has around 20000 post it gets slow at generating pagination. i have traced the problem.

$my_query = new WP_Query($query_string ."&posts_per_page=-1");
$total_posts = $my_query->post_count;

is there any other faster way to get total post on a selected category.

share|improve this question

3 Answers 3

I have one solution for you. This code will show and unordered list with the name of the category and the count of posts containded in it.

<ul class="myClass">
 <?php $categories = get_categories('number=100');
       foreach ($categories as $cat) {
       echo "<li>". $cat->cat_name . " Total posts: ". $cat->category_count ."</li>";
       } ?>
</ul>

Retrieves the 100 first categories

$categories = get_categories('number=100');

and for each of them shows cat_name and category_count

Well the result I presume you really need can now be handled in a var by $cat->category_count, i.e:

$total_num_posts_in_your_category = $cat->category_count

I hope that could help you.

share|improve this answer

It seems there isn't one. The default WordPress count function does not support category posts count:

http://codex.wordpress.org/Function_Reference/wp_count_posts

share|improve this answer

you can use get_category :

 $thisCat = get_category(get_query_var('cat'),false);
$total_posts = $thisCat->count;
share|improve this answer
    
Is there really a function named count that does nothing than count( $thisCat )? –  kaiser Jul 3 '11 at 21:30
    
I'm not sure i get the question, but if you're refering to the count php function, well that is not the case here. –  Bainternet Jul 3 '11 at 23:36
    
My failure. Haven't seen that it gets delivered with the object. –  kaiser Jul 4 '11 at 1:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.