
3 minutes
How to Get WooCommerce Category ID
Let us take a look at how to find the WooCommerce category ID. In web development, you might want to display your products category on the front-end via a shortcode or just highlight a specific cluster of goods, so it is important to understand how to identify the ID posthaste. Thankfully, finding the WooCommerce category ID is rather straightforward. We will have a look at the most common methods for accomplishing this task.
WooCommerce Category ID via URL
The simplest approach to identify the ID is from your WordPress dashboard URL. Start with locating the Products tab in your dashboard, click on it and open the Categories window.
Now, pick a category from your list and open it.
Have a glance at the URL in your web browsers tab. Identify a string like this one: tag_ID=21. In our case, the WooCommerce category ID is 21.
That is it, look how easy it was. Indeed, there is no need to involve coding if the number of categories is small or if you do not need to extract additional data from categories.
WooCommerce Category IDs via PHP Query
As we mentioned before, you might want to get all WooCommerce category IDs with a single action. With knowledge in coding and PHP queries, you can obtain this information by calling upon a function to display this information. Start with locating the Theme File Editor under the Appearance tab in your dashboard.
Always use the child theme for your modification purposes. It is a necessary practice to avoid conflicts within the code and keep the main themes files safe. Now, open the functions.php file and paste the following code.
// Get Current Category ID WooCommerce add_action('wp_head', 'get_current_category_id'); function get_current_category_id(){ $current_category_object = get_queried_object(); echo $current_category_object->term_id; }
The PHP query will display the category ID on top of the page. You will be able to see the particular category the specific product has.
Final Words
Even though there are various ways of finding the WooCommerce category ID that involve coding, in most cases, it is not necessary. With a few simple clicks of a button, you will be able to get the category ID almost instantly. If you do decide to implement solutions that involve tinkering with theme files, then keep in mind to take precautions against possible code conflicts. We wish you a bug-free journey through the world of eCommerce, and stay tuned for more articles!
KEEP YOUR ONLINE SHOP SAFE WooCommerce Maintenance Service
Save 17% with our Annual pricing plan.