Support

Account

Home Forums ACF PRO get all fields by group id

Solving

get all fields by group id

    • endcore

    • April 26, 2015 at 1:46 am

    hi,

    im building a very complex template for products.

    it is possible to retrieve all fields for a given group id?

    and is there an easier way to retrieve all groups for a given post type?

    my current code:

    function get_acf_field_groups($post_type) {
    	$built_in = array('group_553b825363a7d', 'group_553b6f6e9891b', 'group_552fbd209f99e', 'group_552bd329a6457');
    	$output = array();
    
    	$args = array (
    		'post_type'			=> 'acf-field-group'
    	);
    
    	$groups = get_posts($args);
    
    	if($groups) {
    		foreach($groups as $group) {
    			if(in_array($group->post_name, $built_in))
    				continue;
    
    			$cond = unserialize($group->post_content);
    
    			if($cond) {
    				foreach($cond['location'][0] as $location) {
    					if('post_type' == $location['param'] && '==' == $location['operator'] && $post_type == $location['value']) {
    						$fields = get_acf_fields_by_group($group->ID); // ... searching for a function like this
          					$output[$group->ID] = $fields;
          				}
    				} 
    			}
    				
    		}
    	}
    
    	return $output;
    }

    thanks.

    greetings

  • You can get all ACF field groups by using the function acf_get_field_groups()

    You can get all of the fields in a group by using the function acf_get_fields($field_group_key)

    Getting all of the groups on a specific post type you’d need to loop through the field groups and look at the location rules.

  • I’m trying to get all fields of a specific group for a very long time now. What I need is to get all fields assigned to a specific custom post type, or if not possible, all fields inside a field group, either by the title or by the post id of the group. Or at least anyhow.

    Could anyone please provide a few bits of code for this? I am almost at wits end. Please?

    I can’t believe that there is no internal function for such a common task. Imagine to create a post from the front end, how to fill the appropriate acf fields? Or imagine a plugin setting, that defines something around this post creation, like which fields are visible? All these tasks need to know which fields are associated.

    Every helping hand is truly appreciated.I searched through the documentation and the forums for several times, and I almost googled myself to death.

  • As far as I know, no, there isn’t any function that will automatically get what you’re looking for. You might be better off posting a new topic rather than adding to another question. It will be seen by more people if it’s in a new thread with no replies.

    If you want to get all the fields assigned to a post type then you can get all the field groups with the function I posted above. Then you can loop through all the field groups and look at the location rules to see if they match the post type you’re looking for.

  • Just came across this and got all the fields for a specific field group using the below.

    if(function_exists('acf_get_field_groups')) {
                    $fieldGroup = acf_get_field_group(11);
                    $fields = acf_get_fields_by_id(11);
                }
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.