Home › Forums › General Issues › Visitor to Query Between 2 Numbers
I have a custom post type called Members.
Each member has their height (in CM) stored using a custom field named “height”.
I want the front-end user to be able to search members between two height values. For example, show all members between 120cm and 150cm.
This will be an addition to my pre-existing faceted search which queries the database using GET parameters in the URL string. For example: ?gender=male&eye_colour=blue
So ideally, the addition of querying height will add something along the lines of &min_height=120&max_height=150
Although I currently have no idea how to get this off the ground.
Any help, much appreciated.
I’m wrapping up this project now but still haven’t found a way to make this happen.
I have successfully managed to query custom fields dynamically using GET parameters by following the instructions at the bottom of this page: view-source:http://www.advancedcustomfields.com/resources/query-posts-custom-fields/
However, how can I return posts with a custom field between two values? IE: ?min-height=50&max-height=150
Many thanks 🙂
You would use the BETWEEN
variation of the meta_query, and you would need to use a meta_query instead of the simple meta_key, meta_value parameters in your pre_get_posts filter (i’m assuming that’s what you meant the you said bottom of the page you linked to)
$meta_query = array(
array(
'key' => 'height',
'value' => array($min_height, $max_height),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$query->set('meta_query', $meta_query);
@hube2, A huge thanks. That got it working.
Here’s my full function that may help other people in the future.
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// allow the url to alter the query
if( isset($_GET['min_height']) ) {
// Load min_height and max_height as veriables
$min_height = $_GET['min_height'];
if ($_GET['max_height'] !='') {
$max_height = $_GET['max_height'];
} else {
$max_height = 9999999;
}
// Query
$meta_query = array(
array(
'key' => 'mws_height',
'value' => array($min_height, $max_height),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$query->set('meta_query', $meta_query);
// Order by height
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'mws_height');
$query->set('order', 'ASC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
The topic ‘Visitor to Query Between 2 Numbers’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.