10
votes

I need your help.

My question is what is the difference between get_terms and get_the_terms in WordPress ?

I know that get_the_terms for example give me set of categories that a post attached to them, but really I can't understand what get_terms do and how it different from get_the_terms ?

thank you all.

3

3 Answers

20
votes

In the WordPress Codex you can find:

For get_the_terms: "Retrieve the terms of the taxonomy that are attached to the post." http://codex.wordpress.org/Function_Reference/get_the_terms

For get_terms: "Retrieve the terms in a taxonomy or list of taxonomies." http://codex.wordpress.org/Function_Reference/get_terms

So, as you said, get_the_terms() will get the terms (e.g. categories) attached to a post, whereas get_terms() will retrieve the terms in a taxonomy (e.g. categories in the category taxonomy). For example, get_terms( 'category' ) will return all categories that you have added to your WordPress site.

0
votes

In a more practical understanding, you can use get_terms outside of the loop, whilst you can only use get_the_terms inside of the loop.

-3
votes

In get_the_terms you can fetch terms by a particular post_id, but in get_terms you can get set of values for set of post_id (ie, post) .