timothyjensen/acf-field-group-values

检索指定 ACF 字段组的所有文章元数据和选项值

3.4.0 2022-08-23 13:29 UTC

README

PHP Version

ACF 字段组值

此组件为 ACF 的 get_field() 函数提供了一个方便的替代方案。它可以作为 WordPress 插件安装,也可以在你的主题或插件中作为依赖项(推荐)使用。

要求

  • PHP 7.3+

安装

安装此组件的推荐方法是使用 Composer

composer require timothyjensen/acf-field-group-values

或者,你可以下载最新版本,并将其安装为典型的 WordPress 插件。

用法

从版本 2.0.0 开始,$config 参数必须包含 acf 字段组导出中的所有数据。这是一个破坏性更改。现在,你应该传递 $config 而不是 $config['fields']。这对于处理克隆字段尤为重要。

  1. 配置 ACF 以在主题或插件中保存字段组 JSON 文件。接下来,将 ACF 字段组 JSON 转换为数组,并将其传递给辅助函数 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 );
  2. 构建包含指定字段组所有文章元数据的数组

    <?php
    
    $acf_post_meta = get_all_custom_field_meta( get_the_ID(), $config );
  3. 构建包含指定字段组所有选项值的数组

    <?php
    
    $acf_option_values = get_all_custom_field_meta( 'option', $config );
  4. 构建包含指定字段组所有术语元数据的数组

    <?php
    
    $term_id = 'term_2';
    
    $acf_term_values = get_all_custom_field_meta( $term_id, $config );
  5. 构建包含指定字段组所有用户元数据的数组

    <?php
    
    $user_id = 'user_2';
    
    $acf_user_values = get_all_custom_field_meta( $user_id, $config );
  6. 构建包含指定字段组所有 ACF 块数据的数组

    <?php
    // The $block variable is passed to the render callback or template.
    $data = $block['data'];
    
    $acf_block_data = get_structured_block_data( $data, $config );
  7. 为了检索克隆字段的值,你必须传递第三个参数:包含要克隆的字段的字段组数组。

    <?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 );
  8. 从版本 3.2 开始,你可以在结果中包括字段标签和值。

    <?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',
            ],
        ],
    ]
    */

示例结果

在下面的测试结果中,get_all_custom_field_meta()get_field() 快 600%,并且数据库查询减少了 19 次。请注意,返回的值是原始元数据值,而不是由 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',
    ],
];