0

I have a custom post called person and a taxonomy(like tags) called age. As far I have search tax query doesn't seem to support the between operator like meta query does.

Is there a way to accomplish the between, like etc operators with tax query or do I have to make my own sql question ?

Also would be better to create a custom field called age rather have a taxonomy and query with meta query that supports the compare operators ? One disadvantage I can think with using custom fields is that you can't query inside the admin panel how many people have that age or query only the people that have a certain age etc. Also if I want to find the min and max age of all people I have to get all posts and compare them one by one.

One solution I found to emulate the between operator :

$min_age = 18;
$max_age = 100;
$ages = array();
for($i=$min_age; $i<=max_age; $i++){

   array_push($ages , $i);

}
$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'age',
            'field' => 'slug',
            'terms' => $ages
        )
    )
);
$query = new WP_Query( $args );

So what do you think is better to do ?

I know my question is a bit vague and sorry for that but I would like to know what is the best solution to this kind of situations.

1

I think that you are using the custom taxonomy with a incorrect concept. You should use a custom field if you want sort by its value.

Thinking in numbers can be dificult to understand what a taxonomy is. But think in a way to group things where a group is not higher or lower than other. Every term in a taxonomy has the same value, except in hierarchical taxonomies. For example, think in animal taxonomy. All Genus are in the same level, all under the same parent Family, and all Families are in the same level under the same parent Order. How can you sort by Genus or search in between Genus?. You only can search animals if they are IN/OR/NOT IN/AND in one or more Genus. A taxonomy is definetely not for for values that are going to use to sort by (a searh IN BETWEEN is a type o sort by).

You could build a hierarchical taxonomy for each integer number that represent an age, but even in this case, you have to group by a taxonomy term and them recursively search for children/parent items, what is not really a sort by/search between value. I think is not practical if compared with the idea ofe age as meta data (ie custom field).

  • Thanks a lot! I got it now. Your example with animals was very clear. – Laxmana May 5 '14 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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