Giter Site home page Giter Site logo

75th / acf-field-group-values Goto Github PK

View Code? Open in Web Editor NEW

This project forked from timothyjensen/acf-field-group-values

0.0 2.0 0.0 73 KB

Retrieve all post meta, term meta, and option values for the specified ACF field group.

PHP 91.73% Shell 8.27%

acf-field-group-values's Introduction

PHP Version Build Status codecov

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+

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.

  1. 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 );
  2. 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 );
  3. Build an array containing all option values for the specified field group:

    <?php
    
    $acf_option_values = get_all_custom_field_meta( 'option', $config );
  4. 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 );
  5. 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 );

Example Results

In the test results below get_all_custom_field_meta() was 600% faster than get_field() and required 19 fewer database queries.

<?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',
		],
	],
	'clone'            => [
		'group_1'  => 'Cloned group 1',
		'subgroup' => [
			'subgroup1' => 'Cloned subgroup 1',
			'subgroup2' => 'Cloned subgroup 2',
		],
	],
];

acf-field-group-values's People

Contributors

timothyjensen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.