Interacting with the new WP_Term_Query

WordPress 4.6 is going to be awesome! From the developer’s point of view, mostly. One of the new improvements version 4.6 will introduce, is the WP_Term_Query class, which will change the way we query and retrieve terms.

 

What’s a Term?

In WordPress, “terms” refer to single item in a Taxonomy. For example, if we have an Animal Taxonomy, each animal is a single term. Grouping a bunch of different types of animal terms will give us the Animals Taxonomy.

You can retrieve terms with the good old get_terms(), which will return an array of objects, where each object is an instances of the WP_Term class.

 

WordPress Terms are Taxonomy Items
WordPress Terms are Taxonomy Items

 

Querying Terms

The freshly-introduced class WP_Term_Query will help us querying terms the same way we are querying WordPress Posts (using WP_Query), WordPress Users (with WP_User_Query) and WordPress Comments (WP_Comment_Query).

The various query classes in WordPress allow you to filter down items from your database, the WordPress way. No need to reinvent the wheel writing your own functions or custom SQL queries. I mean, you could write your own SQL queries but unless you are an expert DBA (and have a deep understanding of WordPress’ underlying systems such as the Caching API), you’ll most probably end up with an inefficient, insecure piece of functionality.

When using WordPress own query classes, you can be at ease – all caching and security measures are done by WordPress core. As with any other system you develop for, it’s a good practice to use internal instruments so you don’t need to reinvent the wheel, while being on the safe side, keeping it future-proof and avoiding security issues.

 

Term Query Generator

To make it even easier to embrace this new query class, we’ve added a new Generator to help you get started using the Term Query. Visit the WP_Term_Query Generator and browse all available arguments, try filtering your terms by taxonomy, by search terms, by descendants or custom fields. You can even try to create efficient queries by returning only the needed fields, not the entire object.

In general see what it offers, and how easier it will be for you to interact with WordPress terms in the future. Prepare yourself to the new coding standards. Learn more about the WordPress Query Classes using our Query Generators. They are all free.

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on whatsapp
Share on email
Rami Yushuvaev

Rami Yushuvaev

I'm an entrepreneur, a web developer and a blogger. I’ve contributed code to each and every release since WordPress 2.8. I'm the Hebrew GTE responsible for the translation and the release of WordPress Hebrew version. The founder GenerateWP.com and several other WordPress related projects. I work mainly with Israeli startups and hi-tech companies, providing development services.

6 Comments:

Leave a Reply

Subscribe to our Blog

Receive email notifications when new blog posts are published.