Multiple Term Queries

The MasterPress API offers many ways to retrieve multiple taxonomy terms from your WordPress site, with term-related methods available in both the main WOOF class, but also in many other classes across the API. All of these methods return a WOOF_Collection of MEOW_Term objects, rather than simple arrays.

The first way we can access a collection of taxonomy terms is via the WOOF::terms method, which is an abstraction around the WordPress method get_terms, supporting the same query arguments as that function:

Example 1: Accessing terms via a query on $wf

Magic method term queries on $wf

The WOOF has a useful override on the __call magic method to allow you to run a term query on a specific taxonomy type by calling the plural form of the name of the taxonomy with the desired query arguments. So we can improve Example 1 with the following code:

Example 2: Adding some magic to term queries

This is certainly a more intuitive way to run term queries under both custom and built-in taxonomies.

The WOOF class contains a few more presets on the WOOF::terms method, which are listed in the class refrerence.

Accessing terms from a WOOF_Taxonomy object

The WOOF_Taxonomy object contains a whole host of methods for accessing terms under the taxonomy the object represents.

Let’s look at the basic way to access all terms underneath this post type, by simply using the WOOF_Taxonomy::terms method:

Example 3: Accessing terms under a taxonomy

This method is a preset on the WOOF::terms method, which locks in the taxonomy name by way of context. You are free to pass in any of the other supported parameters in the $args parameter.

Magic method access to all terms under a taxonomy

By extending woof-taxomomy from WOOF_Collection and through a useful override on the __get magic method in the WOOF class, we can also access all terms under a taxonomy by accessing the plural form of the name of the taxonomy as a property. So we can improve Example 2 with the following code:

Example 4: Magic term access

Accessing terms from a WOOF_Post object

Another way to lookup a set of terms in the MasterPress API is to first retrieve a single post object, and then request a list of terms attached to that post. Let’s look at an example:

Example 5: Get the ingredients for the current post

This method is not a preset on WOOF::terms but rather, makes use of the WordPress function get_the_terms.

Accessing Terms from a WOOF_Term object

The WOOF_Term class itself has a number of methods for accessing terms that are related to the current term that the object represents. One group of these worth noting here are those concerned with hierarchical taxonomies, where we can access children, siblings, parent, and ancestor term, via the WOOF_Term::childrenWOOF_Term::siblingsWOOF_Term::parent, and WOOF_Term::ancestors methods.

For example, here’s how we could access the children of a category:

Example 6: Accessing child terms

This is a far more intuitive way to access the children than creating the necessary query parameters yourself. Please refer to the class reference for examples of the other hierarchical taxonomy methods linked to above.

Latest From the Blog

MasterPress 1.1.4 now available

MasterPress 1.1.4 is now available. This release contains an important compatibility fix for WordPress 4.5 to allow correct detection of the taxonomy term editing screen. Without this fix, any custom fields you have attached to custom taxonomies will not be shown at all in the editing form. Note also that MasterPress will still detect the edit screen correctly in… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 3.4, MySQL 5, and PHP version 5.2.4.

We also recommend that PHP is configured to use a memory limit of 64MB per request (128MB may be required for sites with higher complexity).

This plug-in is not compatible with the hosted service.