Magento – Magento2: Get category product count which is show in admin after the category name

categorycollection;magento2product-collection

I want to show category product count which is shown in admin after the category name.
As of now I write this code but it's not working properly.

$category->getProductCollection()->count();

Please see the image to get better idea.

Magento Backend

Please see the image to get better idea.

Magento Frontend

enter image description here

Best Answer

Not sure if this is still relevant but answering it anyway. The answer post by @Jean Paul CP is correct. $category->getProductCount(); return exactly the count which shown in the admin panel categories listing. On the other hand $category->getProductCollection()->count(); only returns count of active saleable products in the category.