Support

Account

Home Forums ACF PRO wp query with multiple relation keys

Solving

wp query with multiple relation keys

  • In the wordpress website I am building I have a custom post (cp_course) which has these subfields: course_code, course_duration, period_a_start, period_a_end, period_b_start, period_b_end, period_c_start, period_c_end, period_d_start and period_d_end.

    The search form has these fields: code, duration, datefrom, dateto. User can search by using any of these. I have only implemented the case that the user uses all search fields:

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

    <?php endforeach;wp_reset_postdata();?>

    But it doesn’t seem working. It’s stuck in a loop, already 10 minutes and still searching. What is going wrong?

  • Sorry, but your argument array is nearly impossible to follow. Please re-post and use code tags.

  • 
    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();?>
    
  • The first thing that I notice is your start/end date fields

    
    
                            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)),
    
                            ),
    

    I think you have >= and <= reversed here. For start date the you want to see if start date (value in db) <= value (input date)

  • This is completely untested and just written in text editor on my lunch break but this is how I would start handling this.

    $args = array(
        'post_type'  => 'cp_course',
        'numberposts' =>-1,
        'orderby' => 'ID',
        'order' => 'ASC',
        'meta_query' =>
            array(
                'relation' => 'AND',
            )
    
    );
    
    if($searchterm) {
        $args['s'] = $searchterm;
    }
    
    if($code) {
        $code_args = array(
            'key'   => 'course_code',
            'value' => $code,
        );
        $args = array_push($code_args ,$args['meta_query'] );
    }
    
    if($duration) {
        $dur_args = array(
            'key' => 'course_duration',
            'value' => $duration,
        );
        $args = array_push($dur_args ,$args['meta_query'] );
    }
    
    if($datefrom) {
        $from_args = array(
            'relation' => 'OR',
            array(
                'key'   => 'period_a_start',
                'compare' => '>=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($datefrom)),
            ),
            array(
                'key'   => 'period_b_start',
                'compare' => '>=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($datefrom)),
            ),
            array(
                'key'   => 'period_c_start',
                'compare' => '>=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($datefrom)),
            ),
            array(
                'key'   => 'period_d_start',
                'compare' => '>=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($datefrom)),
            ),
        );
        $args = array_push($from_args ,$args['meta_query'] );
    }
    
    if($dateto) {
        $to_args = array(
            'relation' => 'OR',
            array(
                'key'   => 'period_1_end',
                'compare' => '<=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($dateto)),
            ),
            array(
                'key'   => 'period_b_end',
                'compare' => '<=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($dateto)),
            ),
            array(
                'key'   => 'period_c_end',
                'compare' => '<=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($dateto)),
            ),
            array(
                'key'   => 'period_d_end',
                'compare' => '<=',
                'type' => 'numeric',
                'value' => date("Ymd", strtotime($dateto)),
            ),
    
        );
        $args = array_push($to_args ,$args['meta_query'] );
    }
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘wp query with multiple relation keys’ is closed to new replies.