Product Count in Categories and WP_Query count posts

Product Count in Categories and WP_Query count posts

In this post, I shared how to Display Product Count View in each Category on WordPress WooCommerce and display WP_Query count posts – custom Post Type or default posts in WordPress.

Display particular product category

Tested code on my installation and it works. Just need to add a line of code to check on a particular product category:

add_action( ‘woocommerce_before_shop_loop’, ‘add_product_count_view’, 10);
function add_product_count_view() {
$terms = get_the_terms( $post->ID, ‘product_cat’);
foreach( $terms as $term ) {
if(is_tax(‘product_cat’, $term->name)) {
echo ‘Product Category: ‘
. $term->name
. ‘ – Count: ‘
. $term->count;
}
}
}
Product Count in Categories and WP_Query count posts

Use this parameter to display and count the number of ‘schusslersalz' (example custom post name) posts

The wp_count_posts function has parameter $type for post type to count, you should use this parameter if you want to count the number of schusslersalz:

A snippet
$count_posts = wp_count_posts( ‘schusslersalz’ )->publish;
echo $count_posts;

Full snippet as follow:
$args = array(
‘post_type’ => ‘schusslersalz’
);
$the_query = new WP_Query( $args );
echo $the_query->found_posts;

Reference link for posts count
Reference link for product category count


«

Leave a Reply