$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Can an array be used as a meta_query value?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Can an array be used as a meta_query value?

matteradmin9PV0评论

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!

Post a comment

comment list (0)

  1. No comments so far