timothyjensen/acf-field-group-values
Composer 安装命令:
composer require timothyjensen/acf-field-group-values
包简介
Retrieve all post meta and option values for the specified ACF field group
README 文档
README
ACF Field Group Values
This component provides a convenient alternative to ACF's get_field() function. It can be installed as a WordPress plugin, or required as a dependency (preferred) within your theme or plugin.
Requirements
- PHP 7.3+
Installation
The recommended way to install this component is with Composer:
composer require timothyjensen/acf-field-group-values
Alternatively, you can download the latest release and install it like a typical WordPress plugin.
Usage
As of version 2.0.0 the $config argument must contain all data from the acf field group export. This is a breaking change. Also, you should now pass $config instead of $config['fields']. This is especially important when working with clone fields.
-
Configure ACF to save field group JSON files within your theme or plugin. Next, convert the ACF field group JSON to an array that will be passed to the helper function
get_all_custom_field_meta():<?php // Replace with the name of your field group JSON. $field_group_json = 'group_59e226a200966.json'; $config = json_decode( file_get_contents( PATH_TO_ACF_JSON . $field_group_json ), true );
-
Build an array containing all post meta for the specified field group:
<?php $acf_post_meta = get_all_custom_field_meta( get_the_ID(), $config );
-
Build an array containing all option values for the specified field group:
<?php $acf_option_values = get_all_custom_field_meta( 'option', $config );
-
Build an array containing all term meta for the specified field group:
<?php $term_id = 'term_2'; $acf_term_values = get_all_custom_field_meta( $term_id, $config );
-
Build an array containing all user meta for the specified field group:
<?php $user_id = 'user_2'; $acf_user_values = get_all_custom_field_meta( $user_id, $config );
-
Build a multidimensional array containing all ACF block data for the specified field group:
<?php // The $block variable is passed to the render callback or template. $data = $block['data']; $acf_block_data = get_structured_block_data( $data, $config );
-
In order to retrieve values for clone fields you must pass a third argument: all field group arrays that contain the fields that will be cloned.
<?php // Replace with the names of your field group JSONs. $clone_json_1 = 'group_59e226a200967.json'; $clone_json_2 = 'group_59e226a200968.json'; $clone_fields = [ json_decode( file_get_contents( PATH_TO_ACF_JSON . $clone_json_1 ), true ), json_decode( file_get_contents( PATH_TO_ACF_JSON . $clone_json_2 ), true ) ]; $acf_post_meta = get_all_custom_field_meta( get_the_ID(), $config, $clone_fields );
-
As of version 3.2 you can include field labels along with values in the results.
<?php // Passing 'true' as the fourth argument will include field labels in the results. $acf_post_meta = get_all_custom_field_meta( get_the_ID(), $config, [], true ); /* The above results in: 'group' => [ 'group_1' => [ 'label' => 'Group 1 Label', 'value' => 'Group 1', ], 'group_2' => [ 'label' => 'Group 2 Label', 'value' => 'Group 2', ], 'subgroup' => [ 'subgroup1' => [ 'label' => 'Subgroup 1 Label', 'value' => 'Subgroup 1', ], 'subgroup2' => [ 'label' => 'Subgroup 2 Label', 'value' => 'Subgroup 2', ], ], ] */
Example Results
In the test results below get_all_custom_field_meta() was 600% faster than get_field() and required 19 fewer database queries. Note that the returned values are raw meta values as opposed to formatted values that can be returned by get_field().
<?php $results = [ 'group' => [ 'group_1' => 'Group 1', 'group_2' => 'Group 2', 'subgroup' => [ 'subgroup1' => 'Subgroup 1', 'subgroup2' => 'Subgroup 2', ], ], 'repeater' => [ [ 'repeater_sub_field' => 'Sub Field', 'repeater_2' => [ [ 'repeater_2_subfield' => 'Level 2 subfield', ], [ 'repeater_2_subfield' => 'Level 2 subfield', ], ], ], ], 'flexible_content' => [ [ 'acf_fc_layout' => 'flex_content_type_1', 'flex_content_field' => 'Flex content type 1', ], [ 'acf_fc_layout' => 'flex_content_type_2', 'flex_content_field' => 'Flex content type 2', ], ], // The following are cloned fields. 'group_1' => 'Cloned group 1', 'subgroup' => [ 'subgroup1' => 'Cloned subgroup 1', 'subgroup2' => 'Cloned subgroup 2', ], ];
timothyjensen/acf-field-group-values 适用场景与选型建议
timothyjensen/acf-field-group-values 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.43k 次下载、GitHub Stars 达 47, 最近一次更新时间为 2017 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 timothyjensen/acf-field-group-values 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timothyjensen/acf-field-group-values 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 47
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2017-11-06