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.
https://it.zoetalentsolutions.com/technology-provider/aws/
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(
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(
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. Thanks
course_code
, I wouldn't be surprised if 95% of the time spent was purely because of themeta_query
, those don't scale and get stupendously expensive, as your post count rises the cost rises too. Also, I see yourtax_query
searches for terms via theirterm_id
, but you pass a term slug not a term ID, is this a mistake? I also seeposts_per_page
is set to-1
, it would be safer to set this to a very high number, e.g. 150, otherwise you could accidentally show more posts than there is time to render the page or memory to hold them in