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.

I would like to do a filter that will hook to the main query and filter all the posts by some given taxonomy terms or custom fields. For now I have made the custom filters for the $query to only select the posts that have a custom value for certain taxonomy as follows:

foreach($data as $taxonomy => $terms):
    $query->set($taxonomy,$terms);
endforeach;

and this solves it. But for example, I am having the following issue: I don't see how to provide instead of a taxonomy field, a custom field that has the value in a range; For example price is between 500-1000; where price is a custom field.

I have tried anything I could and searched a lot on the codex but I can't figure it out.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Have a look at the format of a meta_query in WP_Query.

$meta_query = array(
    array(
        'key' => 'price',
        'value' => array( 500, 1000 ),
        'type' => 'numeric',
        'compare' => 'BETWEEN'
    )
);

$query->set( 'meta_query', $meta_query );
share|improve this answer
    
I see. I have checked this now and it seems that after I do th $query->set('meta_query',$meta_query); it seems that by doing a var_dump() on the main query, the 'meta_query' field is set to bool(false); Why' that? –  roshkattu Feb 13 '13 at 17:57
    
sorry, typo - $meta_query => array( should have been $meta_query = array( on the first line. –  Milo Feb 13 '13 at 18:05
    
Yes. I know, I have fixed that. There is no problem. Also, to enable meta_query you have to do this: $query->meta_query = true; so the query will actually use the filters provided! And THANK YOU for explaining me this.. it solved my problem! BIG UP –  roshkattu Feb 13 '13 at 23:21

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.