Home › Forums › ACF PRO › wp query with multiple relation keys › Reply To: wp query with multiple relation keys
if(!empty($code) && !empty($duration) && !empty($datefrom) && !empty($dateto))
{
$args = array(
'post_type' => 'cp_course', 'numberposts' =>-1,'orderby' => 'ID', 'order' => 'ASC', 's' => $searchterm,
'meta_query' =>
array(
'relation' => 'AND',
array(
'key' => 'course_code',
'value' => $code,
),
array(
'key' => 'course_duration',
'value' => $duration,
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'period_a_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_a_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),
array(
'relation' => 'AND',
array(
'key' => 'period_b_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_b_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),//IF I REMOVE the follow lines (period_c_* and period_d_*) then it works
array(
'relation' => 'AND',
array(
'key' => 'period_c_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_c_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),
array(
'relation' => 'AND',
array(
'key' => 'period_d_start',
'compare' => '>=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($datefrom)),
),
array(
'key' => 'period_d_end',
'compare' => '<=',
'type' => 'numeric',
'value' => date("Ymd", strtotime($dateto)),
),
),
),
)
);
$course = get_posts($args);
}
<div class="page_title">
<h3>Courses</h3>
</div>
<?php foreach ($course as $post):setup_postdata($post);?>
<a href="#"><?php the_title();?></a>
<?php endforeach;wp_reset_postdata();?>
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
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.