Support

Account

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();?>