Hello,
I’m trying to query a custom post type (courses) by grade using WP_Query and Advanced Custom Fields (ACF). Grades (checkboxes) are organized by term (groups). Four grades per term, four terms per course.
Here are the ACF fields on the backend:
So depending on how many grade checkboxes are checked, I need the ability to (potentially) query up to four sets of grades. I’ve used the following method (with some success), but if I try and query more than one term’s worth of grades at a time, it seems to overload the database and the page just sits there spinning.
$args = [
'post_type' => 'course',
'division' => 'division-name',
'program' => 'program-name',
'post_status' => 'publish',
'posts_per_page' => '-1',
'meta_query' => [
'relation' => 'OR',
'term_1_grades' => [
'relation' => 'OR',
'term_1_grade_9' => [
'key' => 'term_1_group_term_1_grades',
'value' => '9',
'compare' => 'LIKE',
],
'term_1_grade_10' => [
'key' => 'term_1_group_term_1_grades',
'value' => '10',
'compare' => 'LIKE',
],
'term_1_grade_11' => [
'key' => 'term_1_group_term_1_grades',
'value' => '11',
'compare' => 'LIKE',
],
'term_1_grade_12' => [
'key' => 'term_1_group_term_1_grades',
'value' => '12',
'compare' => 'LIKE',
],
],
'term_2_grades' => [
'relation' => 'OR',
'term_2_grade_9' => [
'key' => 'term_2_group_term_2_grades',
'value' => '9',
'compare' => 'LIKE',
],
'term_2_grade_10' => [
'key' => 'term_2_group_term_2_grades',
'value' => '10',
'compare' => 'LIKE',
],
'term_2_grade_11' => [
'key' => 'term_2_group_term_2_grades',
'value' => '11',
'compare' => 'LIKE',
],
'term_2_grade_12' => [
'key' => 'term_2_group_term_2_grades',
'value' => '12',
'compare' => 'LIKE',
],
],
'term_3_grades' => [
'relation' => 'OR',
'term_3_grade_9' => [
'key' => 'term_3_group_term_3_grades',
'value' => '9',
'compare' => 'LIKE',
],
'term_3_grade_10' => [
'key' => 'term_3_group_term_3_grades',
'value' => '10',
'compare' => 'LIKE',
],
'term_3_grade_11' => [
'key' => 'term_3_group_term_3_grades',
'value' => '11',
'compare' => 'LIKE',
],
'term_3_grade_12' => [
'key' => 'term_3_group_term_3_grades',
'value' => '12',
'compare' => 'LIKE',
],
],
'term_4_grades' => [
'relation' => 'OR',
'term_4_grade_9' => [
'key' => 'term_4_group_term_4_grades',
'value' => '9',
'compare' => 'LIKE',
],
'term_4_grade_10' => [
'key' => 'term_4_group_term_4_grades',
'value' => '10',
'compare' => 'LIKE',
],
'term_4_grade_11' => [
'key' => 'term_4_group_term_4_grades',
'value' => '11',
'compare' => 'LIKE',
],
'term_4_grade_12' => [
'key' => 'term_4_group_term_4_grades',
'value' => '12',
'compare' => 'LIKE',
],
],
],
];
$the_query = new WP_Query($args);
I’ve tried using an array as the meta_query value like this, but it basically bypasses the meta_query arguments altogether and shows unfiltered results:
$args = [
'post_type' => 'course',
'division' => 'division-name',
'program' => 'program-name',
'post_status' => 'publish',
'posts_per_page' => '-1',
'meta_query' => [
'relation' => 'OR',
'term_1_grades' => [
'key' => 'term_1_group_term_1_grades',
'value' => ['9', '10', '11', '12'],
],
'term_2_grades' => [
'key' => 'term_2_group_term_2_grades',
'value' => ['9', '10', '11', '12'],
],
'term_3_grades' => [
'key' => 'term_3_group_term_3_grades',
'value' => ['9', '10', '11', '12'],
],
'term_4_grades' => [
'key' => 'term_4_group_term_4_grades',
'value' => ['9', '10', '11', '12'],
],
],
];
$the_query = new WP_Query($args);
I’ve done a fair amount of searching on the web and nothing I’ve found has really addressed my specific issue. Any help with this would be greatly appreciated!
Thank you in advance!
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.