Filter posts by multiple taxonomies and terms

By default, WordPress allows some limited post filtering options using taxonomies. If you have many taxonomies and you need to filter posts taking into account several taxonomies, and to filter by more than one term, well, there is no direct way to that with WP only.

Intro

GD Custom Posts and Taxonomies Tools plugin adds a function that makes such complex taxonomies filtering possible. This function is available only in Pro edition of the plugin. Function is called gdtt_get_posts(), and you can see some details and simple examples for this function in documentation. Great thing about this function is that beside working as a taxonomies/terms filter, it can accept all the arguments accepted by WordPress get_posts() function. This means that you can specify sorting, post types and everything else supported by default function. So, wherever you need filtering by taxonomies, you can safely replace default function by one added by this plugin.

Documentation on Dev4Press is using this function to filter documents by product and type.

Filter arguments

Our function takes 3 arguments. First is standard arguments array, the same as in get_posts() function. Second, is filter part where you can specify taxonomies and terms. Third specifies what function should return: array with posts or actual WP_Query object. Filter argument is array. Each element in this array is again array that defines filter terms for a single taxonomy. You can have as many taxonomies filter as you want. Each element defines taxonomy and terms to use. Also, you can specify the way terms are added. Terms can be added as slugs or any other database property for terms: slug, term_id or name. Default is term_id.

How to use it

So, you have custom post type Movies (movie). You have taxonomies for it: format (for instance: DVD, Blu-Ray), genre (for instance Horror, Sci-Fi, Drama…), language (for instance: English, French, Italian…) and rating (for instance: PG, R, Unrated).

If you want to get all English R rated dramas and horror movies on DVD, there is no simple way to do it from WordPress. You would need several queries and you will get object that can’t be used as normal WP Query object. With our plugin, this filter can be called like this:

[sourcecode language=”php”]<?php
$args = array("post_type" => "movie");
$filter = array(
array("tax" => "format", "field" => "name", "terms" => array("DVD")),
array("tax" => "genre", "field" => "slug", "terms" => array("drama", "horror)),
array("tax" => "language", "field" => "name", "terms" => array("English")),
array("tax" => "rating", "field" => "name", "terms" => array("R"))
);
$posts = gdtt_get_posts($args, $filter);
?>[/sourcecode]

As you can see, we made filter that includes all the criteria we want for filtering. First, we filter by format, using term name, and term is DVD. Second filters by genre. We use slug form terms, and terms are here as slugs, with lower case and as saved by WP. To get slug versions of terms you can check out taxonomies page. You can always use names, but here is example for slugs also. Third filter is for English language and last for R rating. First parameter in each filter is taxonomy. You must use taxonomy name as defined in taxonomy, NOT label!

Query filtering

If you need to modify default query used for theme template, you can do that also. Normal loop start for template looks like this:

[sourcecode language=”php”]<?php
if (have_posts()) : while (have_posts()) : the_post();
?>[/sourcecode]

To add filter to this loop, before this code you need to add filter. We will use simpler version here with only DVD format as filter.

[sourcecode language=”php”]<?php
global $wp_query;
$args = $wp_query->query_vars;
$filter = array(
array("tax" => "format", "field" => "name", "terms" => array("DVD"))
);
$wp_query = gdtt_get_posts($args, $filter, "query");
if (have_posts()) : while (have_posts()) : the_post();
$wp_query->get_posts();
?>[/sourcecode]

Second line will use global main WP_Query object, that will be replaced by result of our function. Third line will use query arguments already set for the object. You don’t need to do that, and you can specify any array with this as you want as in previous example. Last line will query the object again with new settings.

Conclusion

This is very powerful function that can help you make filter interface and filter down posts by many different criteria in the same time, something WP can’t do by default.

1 Comment

  1. Robert S says:

    Hi Milan,

    Embedded link is post:
    http://www.dev4press.com/docs/?nav=999&doc=1772

    Ends up on a 404 page.

    Cheers,

Leave a Reply