Slow WP_query due to nested wp_query. Need Suggestions


I’m facing an issue of slow queries. As I’m not that familiar with the WP_query, I managed to solve my problem with nested wp_query.

in the above category page URL, you’ll see the Courses and each courses having multiple schedules.
So I’ve 2 post-types 1.Courses & 2.Course_Schedules, every course has a unique course_code(stored in the custom field).

What I’m doing in the above page is:
I used wp_query to get posts and for every post:

    // Custom WP query get_courses_in_cat
$args_get_courses_in_cat = array(
    'post_type' => array('course'),
    'post_status' => array('draft','publish'),
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'title',
    'tax_query' => array(
            'taxonomy' => 'course-category',
            'field' => 'term_id',
            'terms' => array($term->slug),
            'operator' => 'IN',
            'include_children' => false,

I nested another WP_Query in the above query while-loop to get course_schedules by using unique course_code as meta field condition:

    $ThisCourseCode = get_post_meta(get_the_ID(),'course_code',true);
    // Custom WP query get_course_schedules
$args_get_course_schedules = array(
    'post_type' => array('course-schedule'),
    'post_status' => array('draft'),
    'posts_per_page' => -1,
    'nopaging' => true,
    'order' => 'ASC',
    'meta_key' => 'startdate',
    'orderby' => 'meta_value',
    'meta_query' => array(
                    'key' => 'course_code',
                    'value' => $ThisCourseCode,
                    'compare' => '=',


This is working fine but the query is taking so much time to execute.

Solutions which I thought:
Storing unique course_code in button and capturing OnClick using jquery to pass course_code to admin-ajax.php and run wp_query there and show the results here.

Can this ajax request accept parallel queries(if the user clicks on multiple buttons)? or
do I have to pass only one request at a time & show the results in a popup?

Guide me in the correct way to execute this thing, any answers would be appreciated.

, , , , Danish Mohammed 2 years 2020-08-26T10:10:33-05:00 0 Answers 51 views 0

Leave an answer