3

When upgrading our portfolio I'm looking for something that shows the number of posts in a (current) category of a post type.

Example: I have a category with 'features'. Each feature has it's own description, apps related and blog related posts page

I'm fetching the current category with:

<?php $cat_id = get_query_var('cat'); ?>

and get posts with:

 <?php query_posts("post_type=apps&cat=$cat_id&showposts=3");?>

How is it possible to count the posts of post_type=apps and category=$cat_id ?

I'm sorry for my lack of PHP/WP experience.. does anyone know how to get these 2 things working?

Thanks in advance.

1
  • First, don't use query_posts(), second, please ask the second question as new question.
    – kaiser
    Jul 3, 2013 at 11:50

4 Answers 4

9

Public API for the rescue

category is a Taxonomy that's already built in. Therefore it's quite easy to get its term count with get_terms( $tax, array( /* args */ ) );

$terms = get_terms( 'category', array(
    'fields' => 'count',
) );

This will do a pretty fast COUNT (*) query so you don't have to worry about performance.

Alternate solution

You could as well just do a normal call to get_terms() and then alter the selects with the filter:

// The core filter:
apply_filters( 'get_terms_fields', $selects, $args )

// Example callback:
add_action( 'get_terms_fields', 'wpse105174_get_terms_fields_cb', 10, 2 );
function wpse105174_get_terms_fields_cb( $selects, $args )
{
    return array( 't.term_id', 'COUNT(*)' );
}

...where $selects is aan array (which will be imploded to a comma separated string).

Making it even faster

You could then further modify the query and restrict it to only some specific categories (terms/taxons) with a WHERE t.slug = whatever

// The core filter:
apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );

// Example callback:
add_action( 'terms_clauses', 'wpse105174_terms_clauses_cb', 10, 3 );
function wpse105174_terms_clauses_cb( $pieces, $taxonomies, $args )
{
    $pieces['where'] = " WHERE t.slug = whatever";
    // Or do some `str_replace`, `preg_replace`, whatever fits in here

    return $pieces;
}

You could set the $pieces['join'] = ''; as well if you don't need access to the term_taxonomy-table.

Cache it

Finally you could utilize the Transients API to save your results (as serialized array) to the DB, so you don't have to query the count every time you need it.

3
  • Hey Kaiser, Thanks for the detailed answer. Problem is I'm not a PHP'er so most of this part I didn't really get..
    – rwzdoorn
    Jul 3, 2013 at 15:05
  • 3
    @rwzdoorn Well, you got full examples there. Just try it? :)
    – kaiser
    Jul 3, 2013 at 15:38
  • @kaiser, is digging into SQL the only way in WP to do further taxonomy filtering: number of tags used within a certain category (as per your example above)?
    – farinspace
    Jan 4, 2018 at 22:32
2

The count of posts is cached in category object for things like this. In category archivve something like this should be easiest:

$category = get_queried_object();
// $category->count has count

If you are trying to retrieve all posts in category via that loop you should be using posts_per_page=-1, -1 for all and posts_per_page is newer replacement for showposts.

To create links to category archive you can use get_category_link() function.

1

Had some trouble in the beginning but this works for me:

<?php $posts = get_posts("post_type=apps&cat=$cat_id"); 
$count = count($posts); 
 echo "$count"; 
?>
1
  • Did you try one of the recommendations above? Or care to comment on them why they didn't work for you?
    – kaiser
    Jul 3, 2013 at 14:42
-1

You can have post count from $post variable after query_posts as

query_posts("post_type=apps&cat=$cat_id&showposts=3");
echo $post->post_count;

You are in a category page I suppose since you are obtaining category id from get_query_var('cat'). So, what do you really want for more than three posts? can you explain it?

1
  • HI MD, The echo $post->post_count; isn't working for me. We have a 'feature' list of related apps we created. If there are more then 3 i'd like to create a button to 'all apps related to feature X'
    – rwzdoorn
    Jul 3, 2013 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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