Odd results in get_the_terms vs wp_get_post_terms

The question:

I’m trying to make a breadcrumb function and I nearly have it don but one small snag…

When I use:

$categories = get_the_terms( $post->ID, 'product_cat' );

I get back an array of categories to use in a loop, etc. Only problem is that it’s in alphabetical order. (I want it in hierarchical order.)

After some digging, I saw one alternative was to use wp_get_post_terms(), but I must be missing something because when I use this:

$categories = wp_get_post_terms( $post->ID, 'product_cat', array( 'orderby' => 'term_id' ) );

I get an empty array returned.

Any suggestions?

Thanks!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The primary difference between the two is that wp_get_post_terms is not cached and always goes straight to the database, in fact get_the_terms uses the same API internally, wp_get_post_terms is a wrapper around wp_get_object_terms, which is also used by get_the_terms but with caching and fewer configurable options.

However, this is an X Y question. You shouldn’t have asked how to solve your solution with the empty return value, but instead how to solve your problem, aka:

I get back an array of categories to use in a loop, etc. Only problem is that it’s in alphabetical order. (I want it in hierarchical order.)

There is no such thing as hierarchical order in these APIs.

To do this you will need to add a processing step or loops to iterate over the list of terms. e.g. for each term, if it has no parent, display it then loop over all terms in a nested loop displaying those that have this term as their parent, etc. Recursion will be needed if you want more than one layer of depth.

For performance reasons you should use get_the_terms.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment