Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Created August 28, 2017 22:12
Show Gist options
  • Select an option

  • Save zackkatz/80b6f3b55e0b8208d743c60fdd8da871 to your computer and use it in GitHub Desktop.

Select an option

Save zackkatz/80b6f3b55e0b8208d743c60fdd8da871 to your computer and use it in GitHub Desktop.
GravityView - Sort by a Custom Content field value, using the value from another field
<?php
/*
* Tell GravityView to allow sorting on Field #1, for Form #4.
* For example, a Custom Content field that contains another field's value.
*/
add_filter( 'gravityview/sortable/formfield_4_1', '__return_true' );
add_filter( 'gravityview_get_entries', 'gv_custom_sort_by_custom_content_field', 10, 3 );
/**
* Modify the "get entries" criteria to override sorting
*
* @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys.
* @param array $args View configuration args. {
* @type int $id View id
* @type int $page_size Number of entries to show per page
* @type string $sort_field Form field id to sort
* @type string $sort_direction Sorting direction ('ASC' or 'DESC')
* @type string $start_date - Ymd
* @type string $end_date - Ymd
* @type string $class - assign a html class to the view
* @type string $offset (optional) - This is the start point in the current data set (0 index based).
* }
* @param int $form_id ID of Gravity Forms form
*/
function gv_custom_sort_by_custom_content_field( $parameters = array(), $args = array(), $form_id = 0 ) {
//
// SET THIS TO THE ID OF THE CUSTOM CONTENT FIELD YOU WANT TO SORT
//
$custom_content_field_id = 1;
// Update the "4" number below to match the form ID!!!!
if( 4 !== (int) $form_id ) {
return $parameters;
}
// Return: GravityView is not trying to sort by the Custom Content field
if( empty( $parameters['sorting']['key'] ) || $custom_content_field_id !== (int) $parameters['sorting']['key'] ) {
return $parameters;
}
$sort_field_id = 5; // Set the field ID you want to sort by, when sorting the Custom Content field
$sort_direction = isset( $_GET['dir'] ) ? $_GET['dir'] : rgar( $args, 'sort_direction' );
$sorting = array(
'key' => $sort_field_id,
'direction' => strtolower( $sort_direction ),
'is_numeric' => GVCommon::is_field_numeric( $form_id, $sort_field_id ),
);
$parameters['sorting'] = $sorting;
GravityView_View::getInstance()->setSorting( $sorting );
return $parameters;
}
@aliabdy

aliabdy commented Dec 17, 2019

Copy link
Copy Markdown

hi
how i can sort table view by {date_updated} merge tag in custom content field?

@zackkatz

Copy link
Copy Markdown
Author

@al01101001 Set the $sort_field_id to 'date_updated', should work!

@aliabdy

aliabdy commented Dec 18, 2019

Copy link
Copy Markdown

@al01101001 Set the $sort_field_id to 'date_updated', should work!

thank you for you'r response !
and for $custom_content_field_id what should I used?
seems custom content filed don't have any id! or at least I don't see it.
thanks again for you'r help

@jobatkin

Copy link
Copy Markdown

looks like this filter is now outdated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment