we have a couple of custom meta boxes for product (custom post type). one custom meta box key is area_required_width, and we have numbers input for it when publishing products, now we want to have a dropdown including <5, 5-10, 10-20, 20-50, >50 to search this key value of area_required_width at frontend for users.

I know that we can use custom WP_query to search if at backend we store ranges instead of input numbers as frontend. the thing is that our data stores is numbers we input while search criteria is different ranges like <5, 5-10 ...

2 Answers 2


I'm not sure if I fully understand what you're trying to do, but I always recommend the Advanced Custom Fields plugin to people looking to make custom fields more useful and intuitive.

Great plugin with great support and documentation. It really opens up a lot of possibilities.

  • Please make sure your answers address the question :) (don't let this comment put you off posting answers though - it's really appreciated ) May 4, 2012 at 13:29
  • Gabriel, thanks for the reply but i want to code instead of using a plugin.
    – Jerry Woo
    May 4, 2012 at 13:59

It's not clear from the question how you are editing the search to restrict to the meta key (but I trust you're doing it right :) ).

Anyway, WP_Query objects allow you to query posts by ranges in meta value. See Codex.

For example: (I offer this example only as an example of what the meta_query property of a WP_Query object can do. Not as a way of restricting the search query).

$args = array(
    'post_type' => 'product',
    'meta_query' => array(
            'key' => 'price',
            'value' => array( 20, 100 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
$query = new WP_Query( $args );
  • Stephen, thanks for the tip. This only contains situation 20-100, while we have a dropdown selection of <10, 10-20, 20-100, >100 (for example), at the same time we also have other fields like product width. I hope you can understand my description. :)
    – Jerry Woo
    May 4, 2012 at 14:05
  • 1
    I know - you'll have to post the range the user has selected with the search form and then use that to set the value in the above query appropriately. If you need more help, you'll need to post the code that you're using at the moment :) May 4, 2012 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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