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.4.1 is now available

27th May 2024

This update addresses backwards compatibility issues that were introduced in the previous update. There were some edge cases where PHP 7 had issues and it didn’t work at all on PHP 5.6.We have now changed it so that the PHP 8 compatibility changes will now only affect PHP 8 and will be more thorough with… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 4.9, MySQL 5.6, and PHP version 5.6.20.

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 WordPress.com hosted service.

Three AM