Home Forums Add-ons Repeater Field Sort Nested Repeater Fields Using Sub Fields


Sort Nested Repeater Fields Using Sub Fields

  • Hi There!

    I’ve investigated these articles…

    …and also several related topics in the support forum, but none of them quite solve my problem for how I want the fields on a custom post type to behave on my current project.

    I thought I’d reach out and see if what I’d like to achieve is possible with my current Field Group architecture.

    The back story:

    On this project I want to give the client several repeater fields that will allow them to construct pages (based on a custom post type called “sections”), using set blocks of content, in any order they wish within a section.

    Here is my series of repeater fields:

    In some repeater fields, I have nested repeaters, for instance in my “slider” field:

    Each nested repeater has an “Order” number field, which I would like to use to sort each overall field on the page.

    Here’s what I have handling the fields on the template side:

    I thought I could sort the $fields array using the original article referenced above, but I’m missing how to reference the ‘order’ sub field.

    Is this possible with the scope I have set up? What would be the best way to approach this? Any suggestions would be much appreciated. Thanks.

  • Hi @smspaul

    The function $fields = get_field_objects(); will get all fields saved on the page, not the sub fields. Perhaps you could read up on the documentation again as this function is not normally used in this situation.


  • Perhaps also debug your code line by line to see what the values in your variables are. This is an important step in programing as copy/paste will never work as expected.

  • Hi @smspaul

    Thanks again for your work today on the support forum. I just read over your question again, and think I can help a bit more.

    Using the get_field_objects will get you all your repeater fields, this is good and perhaps I oversaw the usefulness.

    I can see what you are doing wiht your setup and I would like to say firstly, that the flexible content field will do all this for you and probably save you some dev time!

    That aside, to sort the data, you will need to merge all the rows together into 1 array like so, and then sort them:

    $rows = array();
    $fields = get_field_objects();
    if( $fields )
    	foreach( $fields as $field )
    		$field_label = $field['label'];
    		$field_name = $field['name'];
    		// loop through repeater rows
    		if( $field['value'] )
    			foreach( $field['value'] as $row )
    				// add extra data to row
    				$row['field_label'] = $field_label;
    				$row['field_name'] = $field_name;
    				// append row
    				$rows[] = $row;
    // test all rows of data
    echo '<pre>';
    echo '</pre>';
    // sort rows
    $order_by = array();
    foreach( $rows as $i => $row ) {
    	$order_by[ $i ] = $row['order'];
    array_multisort( $order_by, SORT_ASC, $rows );
    // test sorted rows
    echo '<pre>';
    echo '</pre>';

    Hope that helps.


  • Hi @elliot,

    Ah thank you! Maybe I will end up using the flexible content field in the future. Seems to be a more streamlined way of handling it for the client. For right now though, this has definitely helped me find my way out of the rabbit hole I was in.

    In any case, thank you very much, I had been staring at this late into the night last night, maybe for a bit too long. 🙂

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

The topic ‘Sort Nested Repeater Fields Using Sub Fields’ is closed to new replies.