Skip to content

Commit ded71e0

Browse files
authored
Create gpnf-sort-child-entries-by-field-value.php
1 parent c5eb074 commit ded71e0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Nested Forms // Sort Child Entries by Field Value
4+
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
5+
*
6+
* Sort child entries by a field value (e.g. name, number, date, etc).
7+
*
8+
* Warning: This does not work for child entries being displayed inside a Nested Form field on the parent form.
9+
* Currently, this is limited to the Nested Entries Simple template but can be easily extended to support other
10+
* post-submission templates. Let us know what you need!
11+
*/
12+
// Update "123 to your parent form ID and "4" to your Nested Form field ID.
13+
add_filter( 'gpnf_template_args_123_4', function( $args ) {
14+
// Update "5" to the ID of the child field by which you would like to sort.
15+
$field_id = 5;
16+
$order = 'asc'; // 'asc' or 'desc'
17+
if ( $args['template'] === 'nested-entries-detail-simple' && $args['field']->is_gravityview() ) {
18+
usort( $args['entries'], function( $a, $b ) use ( $field_id, $order ) {
19+
$first = rgar( $a, $field_id );
20+
$second = rgar( $b, $field_id );
21+
if ( $first == $second ) {
22+
return 0;
23+
}
24+
if ( $order === 'asc' ) {
25+
return ( $first < $second ) ? -1 : 1;
26+
} else {
27+
return ( $first > $second ) ? -1 : 1;
28+
}
29+
} );
30+
}
31+
return $args;
32+
} );

0 commit comments

Comments
 (0)