Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Fields Of Entire Group Advanced Custom Fields

I'm trying to find a way (using the wordpress plugin ACF) to output an entire group of fields.

I have two "Field Groups" and I want to say if either of them is "Active" (with a checkbox) to echo them to my template.

The question is, how do I get an array of all the fields data from one "Field Group"?

Right now I am attempting something like the following:

<?php

$group_ID = 37;

$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

echo "<pre>";
print_r($fields);
echo "</pre>";

?>

It outputs an array of fields but as far as I can tell it contains no actual data:

Array
(
    [0] => Array
        (
            [key] => field_537b79d92081f
            [label] => Active-1
            [name] => active-1
            [_name] => active-1
            [type] => true_false
            [order_no] => 0
            [instructions] => 
            [required] => 0
            [id] => acf-field-active-1
            [class] => true_false
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => null
                                    [operator] => ==
                                )

                        )

                    [allorany] => all
                )

            [message] => 
            [default_value] => 0
            [field_group] => 37
        )

    [1] => Array
        (
            [key] => field_537b78a4c743e
            [label] => Title-1
            [name] => title-1
            [_name] => title-1
            [type] => text
            [order_no] => 1
            [instructions] => 
            [required] => 0
            [id] => acf-field-title-1
            [class] => text
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => field_537b79d92081f
                                    [operator] => ==
                                    [value] => 1
                                )

                        )

                    [allorany] => all
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [formatting] => html
            [maxlength] => 
            [field_group] => 37
        )

    [2] => Array
        (
            [key] => field_537b78c6c743f
            [label] => Info-1
            [name] => info-1
            [_name] => info-1
            [type] => textarea
            [order_no] => 2
            [instructions] => 
            [required] => 0
            [id] => acf-field-info-1
            [class] => textarea
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => field_537b79d92081f
                                    [operator] => ==
                                    [value] => 1
                                )

                        )

                    [allorany] => all
                )

            [default_value] => 
            [placeholder] => 
            [maxlength] => 
            [rows] => 
            [formatting] => br
            [field_group] => 37
        )

)

I would like to basically do the following (excuse the sudo/broken code, PHP isn't my language):

<?php

    $group_ID1 = 37;

    $fields = array($group_ID1);

    if ($fields1['active'] !== null) {
        echo "<h1>";
        echo $fields1['active']['text'];
        echo $fields1['title']['text'];
        echo $fields1['content']['text'];
        echo "<h1>";
    }

    $group_ID2 = 38;

    $fields2 = array($group_ID2);

    if ($fields2['active'] !== null) {
        echo "<h1>";
        echo $fields2['active']['text'];
        echo $fields2['title']['text'];
        echo $fields2['content']['text'];
        echo "<h1>";
    }

?>

I can also get a specific fields data like below:

<?php

    $field_key = "field_537b78c6c743f";
    $field = get_field_object($field_key);

    if($field) {
        echo "<pre>";
        print_r($field);
        echo "</pre>";
    }

?>

Which outputs the following:

Array
(
    [key] => field_537b78c6c743f
    [label] => Info-1
    [name] => info-1
    [_name] => info-1
    [type] => textarea
    [order_no] => 2
    [instructions] => 
    [required] => 0
    [id] => acf-field-info-1
    [class] => textarea
    [conditional_logic] => Array
        (
            [status] => 0
            [rules] => Array
                (
                    [0] => Array
                        (
                            [field] => field_537b79d92081f
                            [operator] => ==
                            [value] => 1
                        )

                )

            [allorany] => all
        )

    [default_value] => 
    [placeholder] => 
    [maxlength] => 
    [rows] => 
    [formatting] => br
    [field_group] => 37
    [value] => testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
)

But do I really need to call each field key for each "Field Group"? Is there not an array of all the fields in one "Field Group"?

like image 742
Thomas Avatar asked May 21 '14 09:05

Thomas


People also ask

How do I get ACF group fields?

Get All Field Group Fields This function get all fields for a given field group using the acf_get_fields function from ACF. The only variable is on line 5, which will hold the field group ID. You can see this in the URL when editing a field group. While looping through all the fields in the group, we check for a value.

How do I export advanced custom fields?

To export Advanced Custom Fields, go to WP All Export › New Export and select the post type or custom post type attached to your ACF fields. Set up the export using drag & drop, then process the export and proceed to download the export file.

How do you use group fields in ACF?

The Group field uses both the parent and child field names when saving and loading values. For example, a Group field named 'hero' with a sub field named 'image' will be saved to the database using the meta name 'hero_image'.


2 Answers

You can get array of all the fields, belonging to one group. The function is:

$acf_fields = acf_get_fields_by_id( $group_ID );

We need to know ACF field group ID. You can find it in url on group edit page, for example:

http://site.ru/wp-admin/post.php?post=340&action=edit

In this case group ID is 340. If you don't want to use hardcoded ID (if your groups are changing from time to time), you can get it, using group name (in this example group name id Technic CPT):

global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );

Output wil look like this:

array(1) {
  [0] => array(29) {
    ["ID"] => int(366)
    ["key"] => string(19) "field_55f05295a59ca"
    ["label"] => string(29) "Gallery of technic"
    ["name"] => string(15) "technic_gallery"
    ["prefix"] => string(0) ""
    ["type"] => string(7) "gallery"
    ["value"] => NULL
    ["menu_order"] => int(6)
    ["instructions"] => string(155) "Field instructions"
    ["required"] => int(0)
    ["id"] => string(0) ""
    ["class"] => string(0) ""
    ["conditional_logic"] => int(0)
    ["parent"] => int(340)
    ["wrapper"] => array(3) {
      ["width"] => int(50)
      ["class"] => string(0) ""
      ["id"] => string(0) ""
    }
    ["_name"] => string(15) "technic_gallery"
    ["_input"] => string(0) ""
    ["_valid"] => int(1)
    ["min"] => string(0) ""
    ["max"] => int(10)
    ["preview_size"] => string(9) "thumbnail"
    ["library"] => string(3) "all"
    ["min_width"] => int(475)
    ["min_height"] => int(365)
    ["min_size"] => string(0) ""
    ["max_width"] => string(0) ""
    ["max_height"] => string(0) ""
    ["max_size"] => int(2)
    ["mime_types"] => string(0) ""
  }
}
like image 52
Alhana Avatar answered Sep 25 '22 20:09

Alhana


I used the following approach to get what I wanted:

<?php
    // Get wordpress page all fields
    $post_id = get_the_ID();
    $fields = get_field_objects($post_id);
?>

<?php
    // Remember Widget
    $status = $fields['remember-status']['value'];
    $text = $fields['remember-text']['value'];
    if ($status == TRUE) {
        echo "<div class=\"info-container\">";
            echo "<p>" . $text . "</p>";
        echo "</div>";
    };
?>
like image 43
Thomas Avatar answered Sep 22 '22 20:09

Thomas