Home › Forums › General Issues › Filter posts by custom fields of the related posts ? › Reply To: Filter posts by custom fields of the related posts ?
Here’s what I’ve done so far:
<?php
//filter by custom field
if(isset($_GET['shift']))
$args['meta_query'][] = array(
'key' => 'shift',
'value' => $shift,
'compare' => 'LIKE'
);
//filtered by taxonomy
if(isset($terms1))
$args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $terms1,
'operator' => 'IN',
// Don't know how to filter by related post's custom fields
$proSearch_query = new WP_Query($args);
while ($proSearch_query->have_posts()){
$proSearch_query->the_post();
$result[] = array(
"id" => get_the_ID(),
"title" => get_the_title(),
"rel_university" => get_field('uni')[0]->post_name,
//returns related post's custom field:
"ranking" => get_field('university')[0]->ranking,
);
}
echo json_encode($result);
die();
?>
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.