Home › Forums › Add-ons › Repeater Field › Sort Nested Repeater Fields Using Sub Fields
Hi There!
I’ve investigated these articles…
http://www.advancedcustomfields.com/resources/how-to/how-to-sorting-a-repeater-field/
http://support.advancedcustomfields.com/forums/topic/sort-of-a-repeater/
…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:
http://cl.ly/image/3r3J0o0N3c1i
In some repeater fields, I have nested repeaters, for instance in my “slider” field:
http://cl.ly/image/1M1Q1z1B0t27
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:
http://hastebin.com/mofowulace.php
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.
Thanks
E
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:
<?php
$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>';
print_r($rows);
echo '</pre>';
die;
// 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>';
print_r($rows);
echo '</pre>';
die;
?>
Hope that helps.
Cheers
E
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. 🙂
The topic ‘Sort Nested Repeater Fields Using Sub Fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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.