admin管理员组文章数量:1130349
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 in the CMS ('Term Status' can be ignored for the purposes of this question):
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 just shows the 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!
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 in the CMS ('Term Status' can be ignored for the purposes of this question):
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 just shows the 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!
本文标签: phpCan an array be used as a metaquery value
版权声明:本文标题:php - Can an array be used as a meta_query value? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749098529a2315637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论