2

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

ACF fields screenshot

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!

3
  • While I'm open to correction if I'm wrong, I don't think it's possible for that kind of meta query - at least with regards to the array values. Meta values are stored as strings. When they are arrays, they are serialized (so essentially a string value). If it were to work, I suspect your meta queries would have to look at the serialized representation of the array value.
    – butlerblog
    Commented Dec 14, 2018 at 21:28
  • 1
    I would try to do this with a taxonomy rather than meta data. Querying on a single key is pretty slow, querying on that many keys will grind most servers to a halt. Meta data was never designed for complex queries, it was sort of tacked on.
    – Milo
    Commented Dec 15, 2018 at 4:17
  • Thank you both. I will take your suggestions/comments into consideration. Regarding using a taxonomy rather than meta keys, I had a similar thought, but was trying to keep my taxonomies clean, but this might be my only option.
    – Sean Vinci
    Commented Dec 17, 2018 at 18:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.