Do you know what categories are in WordPress ? As you are reading this article, I am assuming your answer is yes. You may think that why we are talking about categories. Because get_terms and_get_terms functions are related to categories. But the function doesn’t look related to categories by the name. Why so, you will learn everything throughout this article.
What is get_terms and get_the_terms in WordPress ?
Before you get to know anything about get_terms and get_the_terms. You have to know what the meaning of the term is. People often get confused here. So don’t let the name fool you in any way. In WordPress there are three names that are used for category. You may think, why three names for a single thing ? The reason behind this is to specify categories in a better way.
Next time, whenever someone says terms, taxonomy, or categories. You know what that means.
What is get_terms Function in WordPress ?
It retrieves the terms in a list of taxonomy or given taxonomy. It gives you the power to customize queries fully before sending them. Not just this, you can also control the output with a filter. Although the return depends on the parameters you pass. In case you pass the invalid request. In such situations, the WP_error object will be returned.
Is this too technical to understand? Don’t worry, it’s just a function that is used to get taxonomy. It does a similar work as a get_category function. You will know everything by the end of this article.
get_terms( array|string $args = array(), array|string $deprecated = '' ): WP_Term|int|string|string|WP_Error
What does get_the_terms function mean ?
get_the_terms function gets the terms attached to a post. In simple words, it gets all the taxonomy elements related to a particular post.
get_the_terms( int|WP_Post $post, string $taxonomy ): WP_Term|false|WP_Error
These are the parameters to pass in the get_the_terms(); function;
- $post : It is the ID of the post. It can also be an object. This functions requires ID to work.
- $taxonomy : It is the name of the taxonomy.
Here are the possible outcomes.
- This will return an array of WP_terms if the query is successful.
- It will return false if we don’t pass any parameters or give post.
- It will return WP_error. If we pass invalid parameters.
What if we want to do the same with custom post types? Can we do this with get_terms function. The answer is yes, absolutely. Lets try to learn by example. With the help of get_categories(); function.
$democat = get_terms(array());
If you examine the example, you will find that we haven’t passed any parameters in the array. What do you think what will happen here ? Yes, you are thinking right. This will fetch posts.
$democat = get_terms(array(‘taxonomy’=>’news_category’));
This happens when you want to fetch custom taxonomy from categories that have posts. But what if you want to fetch all the taxonomy even if the category doesn’t have posts. Let’s see an example.
$democat = get_terms(array(‘taxonomy’=>’news_category’),’hide_empty’=false);
Here what happened is we have set the hide_empty categories to false. What does that mean? This means the categories that don’t have posts are no longer hidden. By default, the value for hide empty posts is set to be true. All I want to show is that it doesn’t matter if the category has a post or not. You can show both without having any trouble.
I hope this article helped you understand the get_terms and get_the_terms better. I tried to make things simpler for you. With this I covered all the things related to get terms function. If you find this article helpful, you can check our other articles on wp_nav_menu and get_attachment_image as well. Thank you for reading the article.