Home › Forums › General Issues › How to show related posts based on custom fields? › Reply To: How to show related posts based on custom fields?
To do this you’re going to have to do a new WP_Query on these meta values. Something like this.
$district = get_field('district');
$price = get_field('price');
$low = $price-50;
$high = $price+50
$args = array(
'post_type' => 'your-post-type',
'post_per_page' => 3,
'meta_query' => array(
array(
'key' => 'district',
'value' => $district
),
array(
'key' => 'price',
'value' => $low,
'compare' => '>='
'type' => 'NUMERIC'
),
array(
'key' => 'price',
'value' => $high,
'compare' => '<='
'type' => 'NUMERIC'
)
)
);
$query = new WP_Query($args);
see https://codex.wordpress.org/Class_Reference/WP_Query
also https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.