Support

Account

Home Forums Front-end Issues How to Display all fields in a group

Solved

How to Display all fields in a group

  • Hi,

    I have a series of Field Groups, each attached to a specific product category (woocommerce) and I need to be able to access ALL the data regarding each field group on the product category page. This is to be able to dynamically create multi-select dropdowns to use in filtering items in that category.

    The closest I’ve been able to accomplish is using this function (for debugging):

    function listFields(){
        $fields = get_field_objects();
    
        foreach ($fields as $field => $data){
            echo '<p>';
            echo '<b>Label: '.$data['label'].'</b><br />';
    
            foreach ($data['choices'] as $key => $value ){
                echo 'KEY: '.$key.' VALUE: '.$value.'<br />';
            }
            echo '</p>';
        }
    
    }

    This code has a major problem though that I can’t figure out. It does show categories and the options I need, but it only shows 2 fields in the group.

    It runs within a widget shown on all product category pages..

    Can someone help me please? this is driving me crazy…

  • $fields = get_field_objects(); requires a post ID, if none is supplied then it returns values for the current post. Exactly what that values is depends on the page it’s running. So, the field in question for the current post in your case only has 2 values. These posts have information on how you can get fields in specific field groups.

    ACF5
    https://support.advancedcustomfields.com/forums/topic/get-all-fields-by-group-id/
    https://support.advancedcustomfields.com/forums/topic/how-to-get-all-acf-groups-or-field-for-specific-post-type/
    https://dream-encode.com/acf-get-all-fields-in-a-field-group/

    ACF4
    https://support.advancedcustomfields.com/forums/topic/get-fields-in-a-group-or-tab-for-output/

  • thanks. I’ve seen some of those before in my search and they didn’t work. There’s a new one I haven’t seen yet though. I’ll try that and let you know. Thanks again! I hope it works!

  • this is the code I got working for anyone interested…

        private function display_field_selects(){
            $field_group_key = 'group_5b9b435363912';
    
            $fields = acf_get_fields($field_group_key);
            echo '<form id="category_filter">';
            foreach ($fields as $field => $data){
                echo '<div class="category_wrapper">';
                echo '<p class="field_label">'.$data['label'].'</p>';
                echo '<select id="ms_'.$data['name'].'" multiple>';
    
                foreach ($data['choices'] as $key => $value ){
                    echo '<option value="'.$value.'">'.$key.'</option>';  
                }
                echo '</select>';
                echo '</div>';
            }
            echo '</form>';
        }

    This pulls the correct field group data and creates a dropdown select for each field and its options. I believe it will work only on Radio and Checkboxes.

    The next phase for me now is to either get the field_key automatically or create a function that can retrieve the appropriate field_group_key from a predetermined list.

    Thanks John!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How to Display all fields in a group’ is closed to new replies.