Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

  • price
  • age
  • vintage

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

  • tax.jenswedin.com/age/10,20/
  • tax.jenswedin.com/?age=10,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

  • tax.jenswedin.com/age/10,11,12,13,14,15,16,17,18,19,20/

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

  • 11-20
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 21-30
    • ...
    • ...

By doing this I could now query like this

  • tax.jenswedin.com/age/11-20/

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.

  • chwisgi.com/?age-min=20&age-max=30

Any help would be appreciated.

share|improve this question

1 Answer 1

You could take the query var and programatically expand the numbers between your minimum value and maximum value.

Assuming http://tax.jenswedin.com/age/10,20/ is rewritten to http://tax.jenswedin.com/?age=10,20, then (pseudo code)

$parts = explode(get_query_var('age'), ',');
$min_val = $parts[0]; // Should be 10 in this example
$max_val = $parts[1]; // 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.