skip to Main Content
WooCommerce: Get Top Level Category

Request

Get the id of the top level category for a product no matter what it’s depth in the category tree.

Required Plugins

Code

Thinking

I tried a multitude of different built in WordPress and WooCommerce functions to get me to the top, but none of them actually did what I needed it to do which was return the id of the top-level category that a product might live in.

I needed this to work for any depth of categories, and a HUGE dependency is that it’s written to view the first category comes out of the get_the_terms call.

Disclaimer

Maje Media LLC cannot be held responsible for the functionality of this code. Please make sure you test it on a development site before adding the code to your production website. There is no support available for this (and other) code snippet(s) posted on this website. If you’d like Maje Media to do custom development to help with your custom implementation please send a contact request.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top