首页 > 解决方案 > 为 Wordpress 的高级自定义字段 (ACF) 中的组字段编写 meta_query

问题描述

我认为 ACF 文档在如何查询使用高级自定义字段创建的组的子字段方面非常稀疏。

例子:

acf_custom_dates_group
acf_custom_date_group_child

如何对子字段中的值执行 meta_query?

标签: phpwordpressadvanced-custom-fields

解决方案


这里的诀窍是结合组和子字段键名。我相信 LIKE 运算符也是必要的。

/*
The name of my custom group: acf_custom_dates_group
The name of my sub field: acf_custom_date_group_child

Name combined as 'key' and separated by underscore (see below)
*/

$format_value = ''; // set by ajax
$myquery = new WP_Query([
    'posts_per_page' => -1,
    'post_type' => 'posts',
    'meta_query' => [
        //'relation' => 'OR',
        //'relation' => 'AND',
        [
            'key' => 'acf_custom_dates_group_acf_custom_date_group_child',
            'compare' => 'LIKE',
            'value' => $format_value
        ],
    ],
]);

推荐阅读