NOTE: The links will work If you copy & paste them. (I’m a newbie so I am not allowed to add to many links to the question)
I have a few taxonomies that I would like to query a range from, for example
I found this question which helped me some
How to pass URL parameters for advanced taxonomy queries with multiple terms for one custom taxonomy
With the above examples I could query the age taxonomy as an example for the year of 10 and 20
This works ok.
But if I would like to do an query for an an range of ages, for example form 10-20 I would need to
And if I would like to do a range between 10-100 you can imagine it will will not look that good.
So I tried to put the age terms in sub taxonomy terms so the structure would be like this
By doing this I could now query like this
And it would work, it would show all ages within 11-20
But this way is quite inflexible, I would need just range between 15-20 this wouldn’t work.
What I would like to know if there is a way that the range is dynamic and more flexible. I know that @scribu had a plugin (which isn’t supported longer) that had the had support for ranges with the min & max for custom fields. But I haven’t seen anything like this for taxonomies.
Any help would be appreciated.
You could take the query var and programatically expand the numbers between your minimum value and maximum value.
$parts = explode(get_query_var('age'), ','); $min_val = $parts; // Should be 10 in this example $max_val = $parts; // Should be 20 in this example $range = range($min_val, $max_val); // http://php.net/manual/en/function.range.php /* Blah blah blah lets jump to where you need to query the terms */ $args = array( 'tax_query' => array( array( 'taxonomy' => 'age', 'field' => 'slug', 'terms' => $range ) ) ); $query = new WP_Query( $args );
This works well for things like numbers. But you could do a
foreach loop over the $range array and make the values whatever you need them be.