0

I want to print total number of posts filtered by its category. for example if there are 3 posts in category "abc" and 5 posts in category "xyz". when i am in any post of category "abc" then it should display 3 and if i am in any post of category "xyz" then it should print 5. Thanks

with this piece of code i am struggling to acheive these functionality

foreach((get_the_terms($post->ID, 'project_category')) as $term) { 
echo $term->term_id. ''; 
}

above code will result the term id of current category. suppose the result is 7 then we put it in this code

$term = get_term( '7', 'category' ); 
$count = $term->count;
echo $count;

this will result total number of posts in current category . Now instead of hard coding '7' how can we achieve the result dynamically ?

9
  • Possible duplicate of Count how many posts in category
    – kero
    Jul 19, 2017 at 15:18
  • its not duplicate Jul 19, 2017 at 15:22
  • Why not? Aren't you asking how to count how many posts are in one category?
    – kero
    Jul 19, 2017 at 15:23
  • What code did you use and where did you put it @MuhammadOmarFarooq
    – Charles
    Jul 19, 2017 at 16:06
  • You should really edit your post to include the code from your comments in a code block so we can actually read it.
    – Cedon
    Jul 19, 2017 at 18:33

1 Answer 1

0

I think this will do

foreach((get_the_terms($post->ID, 'project_category')) as $term) { 
   echo $term->term_id, " -> ", $term->count,  '<br>';
   // if post assigned to more than once category you will have multiple lines 
}

Basically get_the_terms call already returns term object, so you can access all properties, including count

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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