-
Notifications
You must be signed in to change notification settings - Fork 92
gpeb-multi-field-sorting.php: Added snippet for multi field sorting on Entry Blocks.
#1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
| /** | ||
| * Gravity Perks // Entry Blocks // Multi Field Sorting | ||
| * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/ | ||
| * | ||
| * Use confirmation message from Form Settings when editing an entry in GPEB. | ||
| * | ||
| * Instruction Video: https://www.loom.com/share/b9867d2735d44519bf563961e9b30bd2 | ||
| */ | ||
| add_filter( 'gpeb_queryer_entries', function( $entries, $gf_queryer ) { | ||
|
|
||
| // Update the form ID to match your form. | ||
| if ( $gf_queryer->form_id != 933 ) { | ||
|
||
| return $entries; | ||
| } | ||
|
|
||
| // Update the field IDs to match your form. | ||
| $primary_sorting_field_id = '1.6'; | ||
| $secondary_sorting_field_id = '1.3'; | ||
| $sorting_direction = 'ASC'; // 'ASC' or 'DESC' | ||
|
|
||
| usort( $entries, function( $a, $b ) use ( | ||
| $primary_sorting_field_id, | ||
| $secondary_sorting_field_id, | ||
| $sorting_direction | ||
| ) { | ||
| $a_primary = isset( $a[ $primary_sorting_field_id ] ) ? $a[ $primary_sorting_field_id ] : ''; | ||
| $b_primary = isset( $b[ $primary_sorting_field_id ] ) ? $b[ $primary_sorting_field_id ] : ''; | ||
|
||
|
|
||
| $cmp = strcasecmp( $a_primary, $b_primary ); | ||
| if ( $cmp === 0 ) { | ||
| $a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : ''; | ||
| $b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : ''; | ||
| $cmp = strcasecmp( $a_secondary, $b_secondary ); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve comparison logic for different data types. The current implementation uses strcasecmp which is only appropriate for strings. Add support for numeric and other data type comparisons. - $cmp = strcasecmp( $a_primary, $b_primary );
- if ( $cmp === 0 ) {
- $a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : '';
- $b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : '';
- $cmp = strcasecmp( $a_secondary, $b_secondary );
- }
+ // Compare primary values based on data type
+ if ($is_numeric) {
+ $a_primary = (float) $a_primary;
+ $b_primary = (float) $b_primary;
+ $cmp = $a_primary <=> $b_primary; // Spaceship operator for numeric comparison
+ } else {
+ $cmp = strcasecmp( $a_primary, $b_primary );
+ }
+
+ // If primary values are equal, compare secondary values
+ if ( $cmp === 0 ) {
+ $a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : '';
+ $b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : '';
+
+ // Determine if secondary values are numeric
+ $is_secondary_numeric = is_numeric($a_secondary) && is_numeric($b_secondary);
+
+ if ($is_secondary_numeric) {
+ $a_secondary = (float) $a_secondary;
+ $b_secondary = (float) $b_secondary;
+ $cmp = $a_secondary <=> $b_secondary;
+ } else {
+ $cmp = strcasecmp( $a_secondary, $b_secondary );
+ }
+ }
🤖 Prompt for AI Agents |
||
|
|
||
| return ( $sorting_direction === 'DESC' ) ? -$cmp : $cmp; | ||
| }); | ||
|
|
||
| return $entries; | ||
| }, 10, 2 ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about wrapping all this in a function that takes an array arg so?