Get a category posts count inside a query loop

Hi,

I’m looking for help as I’m building a query loop in which I identify the name of a cpt’s category. Where I’m stuck is that I also would like to display the number of posts inside that category. Unfortunately, no matter what I do I can’t get this to work.

If anyone could let me know how to do this, that would be great !
Cheers,
Franck.

Hi

Maybe this will give you a start.

A few weeks ago I wrote a php program to show the amount of posts in my custom post type.

You can add the code below in a code widget in Bricks. Change the post type and modify the query arguments as needed, i.e. enter the name of the category.

<?php
$args = array(  
'post_type' => 'write here the post type',
'post_status' => 'publish',
'category_name' => 'write here your category name',
'cache_results'  => false);
$loop = new WP_Query( $args ); 
echo "Total Posts found: ".$loop ->found_posts;
?>

You will find all information about this function here:

Cheers

Patric

Hey Patric,

Thanks for the code. I forgot to mention I’m using bricks builder query loop tool to display cards on the website. I’m not sure if I can put this code inside the tool.

Franck.

OK, understood.

Here is a youtube video from Ivan Nugraha explaining how to do that:

Cheers

Patric

Hey Patric,

Thanks a lot for the link, it is indeed what I need !
Franck.

This does not work for the custom taxonomy term :roll_eyes:

Ok, here is a new code I put together:

function count_term_use () {
 $term_id = \Bricks\Query::get_loop_object_id();
  $CategoryTerms = get_term_by('term_id', $term_id, 'category');
  if(!$CategoryTerms){
  return "0 posts found in category";
  } else {
  return $CategoryTerms->count." posts found in category ".$CategoryTerms->name." (".$term_id.")";
  }
}

In the query loop, you have to enter the categories (terms) to be included in the counting:

You can make a text element and use the function

{echo:count_term_use()}

to show the number of posts / items under the category:

count categories2

Result in the front-end:

Cheers

Patric

1 Like

Hey Patric,

Thanks for all that, the code from the YT video did what I needed.

I just had to replace the post_type by the name of my custom post type, and it worked straight, as I did not create a custom taxonomy.

Here’s the final code I used :

function count_term_use () {
$term_id = \Bricks\Query::get_loop_object_id();
$my_posts = get_posts(array(
  'post_type' => 'my_custom_post_type', // SELECT CORRECT POST TYPE
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'category', //taxonomy name
      'field' => 'id', //field to get
      'terms' => $term_id, //term id
    )
  )
));
   return $count = count($my_posts);
}

Cheers mate,
Franck.

2 Likes

Great to hear that Franck.

In case you want to show the count for different post types on different pages, you can use my other code because that one works without having to define a post type in the function.

All the parameters are defined directly within the Bricks query loop and not in the function itself.

Cheers

Patric

1 Like