Support

Account

Home Forums ACF PRO Get all Fields and Subfields from a page (pageID)

Solved

Get all Fields and Subfields from a page (pageID)

  • Hi everybody,

    I’m looking for a way to gather all fields of a page (field, repeaterfields, their repeaterfields and so on and so on … ) into one array, either as objects or data-arrays with “name” and “value”.

    All I know going into this is the pageId. I dont know any “names” or content of any subfields, infact, “names” and “values” is what Im looking for.

    While get_field_objects() and get_fields() will do the trick for firstlevel fields, repeater fields will be returned as an array, and by trying to somehow collect them via key or anything I keep failing and failing and after two days I’m pretty much spent -_____- …

    Is there any way, a simple loop function or anything that could do that? I need a function that I only feed with the pageID and that returns an array of its fields and subfields. And I just kinda cant shake this feeling that this should be rather simple and easy, and that I’m just missing something, but Im simply out of ideas :-S

    Any help would be much appreciated! Thanks in advance,
    ANB_Seth

  • acf get_fields() does get the field names and values of repeaters and flex fields. For Example it returns this for a repeater

    
    [repeater] => Array
            (
                [0] => Array
                    (
                        [field_1] => value 1
                        [field_2] => value 2
                    )
    
                [1] => Array
                    (
                        [field_1] => value 1
                        [field_2] => value 2
                    )
    
            )
    

    Each sub array represents a row and the values are in field name => value pairs just like the top level fields.

    A flex field is returned like this

    
    [flex] => Array
            (
                [0] => Array
                    (
                        [acf_fc_layout] => layout_1
                        [field_1] => value 1
                        [field_2] => value 2
                    )
    
                [1] => Array
                    (
                        [acf_fc_layout] => layout_2
                        [field_1] => value 1
                        [field_2] => value 2
                    )
    
            )
    

    again, of the values of the array is a row of the flex field.

    maybe I’m missing what it is you’re really looking for. I think the real difficulty will be in telling the difference between flex fields, repeaters and other field types that store array values like select and checkbox fields.

  • first up, thanks for the response 🙂 … I’ll try around with it. As for “what im really looking for”, im trying to create an array that holds all data from every field within a page, be it normal or repeaterfields (or repeaterfields within repeaterfields etc.). So in the end the mentioned array should be full of either fieldobjects, that I could extract data from (“name”, “value”, “type” etc.), or little arrays that hold this data, like [“thefieldname”, “thefieldvalue”, “thefieldtype”].

    So Im kinda trying to fill a function like this:

    function getAllFields($pageID) {
    $everyFieldData = array();


    (push fields or [name,value,type] of every field/subfield in $everyFieldData)


    return ($everyFieldData);
    }

    How would I possibly approach or fill a function like that?

  • Do you mean get_field_objects ?

    $fields = get_field_objects( $post->ID );

  • well, no … or kinda. I did try “get_field_objects” (first thing I tried) but trying out the gathered field-objects (“name” and “value”), those objects with repeaterfields only returns arrays for “value”, and I dont know how to further break this value-arrays apart, since those could contain arrays themself and so on.

    I just need every field of a page gathered, be it normal or subfield, and put into one array. So … pretty much the function I described a bit further up … but filled and working XD. Yet I’m starting to think getting this to work will not be as easy as I had hoped :-S ^^

  • Okay, so I have worked myself to this functons which seem to do the trick:

    
    function abGetAllFields($pro_num){
    	$tmpArr = get_field_objects($pro_num);
    	$fillArray = array();
    	foreach( $tmpArr as $tmpFieldObject ) {
    		$fillArray = abGetAllFieldsCycle($fillArray, $tmpFieldObject["name"], $tmpFieldObject["value"]);
    	}
    	return $fillArray;
    }
    
    function abGetAllFieldsCycle($fillArray, $name, $value) {
    	if (is_array($value)) {
    		foreach( $value as $key => $value ) {
    			$fillArray = abGetAllFieldsCycle($fillArray, $key, $value);
    		}
    	} else {
    		array_push($fillArray, [$name, $value]);
    	}
    	return $fillArray;
    }
    
    $myFieldArray = abGetAllFields($pageId);
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.