This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
Use this hook to customize a filter for a View. This hook will only apply to filters you have added in the "Advanced Settings" section of your View. Please note that this hook is not designed to work with post fields or custom fields. A full database call is necessary to make this work with post or custom fields.
Usage
add_filter('frm_where_filter', 'filter_custom_display', 10, 2); function filter_custom_display($where, $args)
Parameters
- $where (array)
- $args (array) => ['where_opt' (string), 'where_is' (string), 'where_val' (string), 'form_id' (integer)]
Examples
Stop user ID filter for admins
If you are filtering your view to show the user their own entries, but you would like the admin to see all entries, you can remove the query conditionally.
add_filter('frm_where_filter', 'stop_filter_for_admin', 10, 2);
function stop_filter_for_admin( $where, $args ) {
if ( $args['display']->ID == 3 && $args['where_opt'] == 25 ) { //change 3 to the ID of your View and change 25 to the ID of your user ID field
if ( current_user_can('administrator') ) {
$where = "fi.id='". $args['where_opt'] ."'";
}
}
return $where;
}
Filter by User ID in Dynamic field
This code can be used to allow filtering of a View with a Dynamic field that contains UserIDs. This custom filter will make sure the User ID in the Dynamic field is equal to the current user. This code will only be used if you have added a filter to your View that says "Dynamic field is equal to custom".
add_filter( 'frm_where_filter', 'filter_by_linked_id', 10, 2 );
function filter_by_linked_id($where, $args){
if ( $args['display']->ID == 3 && $args['where_opt'] == 25 ) { //change 3 to the ID of your View and change 25 to the ID of your Dynamic field
global $wpdb, $user_ID;
$entry_id = $wpdb->get_col( $wpdb->prepare( "Select id from {$wpdb->prefix}frm_items where user_id=%d and form_id=%d", $user_ID, 5 ) );
//Change 5 to the id of the form linked through your data from entries field
if ( is_array( $entry_id ) && count( $entry_id ) == 1 ) {
$entry_id = reset( $entry_id );
}
if ( ! $entry_id ) {
$where = "meta_value=1 and meta_value=0 and fi.id='" . $args['where_opt'] . "'";
} elseif ( is_array( $entry_id ) ) {
$where = "meta_value in (" . implode( ',', $entry_id ) . ") and fi.id='" . absint( $args['where_opt'] ) . "'";
} else {
$where = '(meta_value = ' . (int) $entry_id . " OR meta_value LIKE '%\"" . (int) $entry_id . "\"%') and fi.id='" . $args['where_opt'] . "'";
}
}
return $where;
}
Check if one of two fields contains a value
This example adds a filter which will check if field x OR field y contains a search term from your URL. In order for this function to be used, you must add a filter to your View which says "Field x is like [get param=search_term]". Replace search_term with the parameter name that you have in your URL. If you would like to search for a value that is equal to the search term, replace 'like' in the code with = and remove the % characters.
add_filter('frm_where_filter', 'custom_or_filter', 10, 2);
function custom_or_filter($where, $args){
if ( $args['display']->ID == 3 && $args['where_opt'] == 100 ) {//Change 3 to the ID of the View. Change 100 to the ID of the field you have added as a filter and holds the data.
$search_val = $args['where_val'];
if ( $search_val ) {
$where = "( (meta_value like '%". $search_val ."%' and fi.id = 100) OR (meta_value like '%". $search_val ."%' and fi.id = 101) )";//Change 100 and 101 to your field IDs
}
}
return $where;
}
Add two filters combined with OR
This example adds a filter which will check if Field A contains value 1 OR Field B contains value 2. The values will be retrieved from a search URL. In order for this function to be used, you must add a filter to your View which says "Field A is like custom". If you would like to search for a value that is equal to the search term, replace 'like' in the code with = and remove the % characters. Replace the field and View IDs with your IDs. Replace the search terms with your search terms as well.
add_filter('frm_where_filter', 'custom_or_filter_two_values', 10, 2);
function custom_or_filter_two_values($where, $args){
$view_id = 4131;// Replace with your View ID
$field_1 = 363;// Replace with ID of Field A
$field_2 = 426;// Replace with the ID of Field B
$search_term_1 = 'test';// Replace with the first search parameter
$search_term_2 = 'test2';// Replace with the second search parameter
if ( $args['display']->ID == $view_id && $args['where_opt'] == $field_1 ) {
$search_val_1 = isset( $_GET[ $search_term_1 ] ) ? $_GET[ $search_term_1 ] : '';
$search_val_2 = isset( $_GET[ $search_term_2 ] ) ? $_GET[ $search_term_2 ] : '';
if ( $search_val_1 && $search_val_2 ) {
$where = "( (meta_value like '%". $search_val_1 ."%' and fi.id = " . $field_1 . ")";
$where .= " OR (meta_value like '%" . $search_val_2 . "%' and fi.id = " . $field_2 . ") )";
} else if ( $search_val_1 ) {
$where = "(meta_value like '%". $search_val_1 ."%' and fi.id = " . $field_1 . ")";
} else if ( $search_val_2 ) {
$where = "(meta_value like '%". $search_val_2 ."%' and fi.id = " . $field_2 . ")";
} else {
$where = "fi.form_id = " . $args['display']->frm_form_id;
}
}
return $where;
}
Filter a field for one of two values
This code allows you to filter a field by one of two values. For example, you can show all the entries where the item purchased is either a book or a toy.
In order for it to work properly, add a filter to your View which says "Field is like custom." If you would like to filter for a value that is equal to the listed values, replace 'like' in the code with = and remove the % characters. Replace the field and View IDs with your IDs and the values with your values.
add_filter( 'frm_where_filter', 'frm_custom_or_filter', 10, 2 );
function frm_custom_or_filter( $where, $args ) {
$view_id = 118;// Replace with your View ID
$field = 165;// Replace with ID of your field
$search_val_1 = 'toy';// Replace with the first value
$search_val_2 = 'book';// Replace with the second value
if ( $args['display']->ID == $view_id && $args['where_opt'] == $field ) {
$where = "( ( fi.id = " . $field . ")";
$where .= " AND ( ( meta_value like '%" . $search_val_1 . "%' )";
$where .= " OR ( meta_value like '%" . $search_val_2 . "%' ) ) )";
}
return $where;
}
Allow content in between search words
You can use this code example to filter entries based on a search term with multiple words (or partial words), where there may be words in between the search words (or partial words).
This code example works with a filter that uses "like" and has [get param] for its value.
For example, there's a filter:
flavor is like [get param=flavor]
The user enters "chocolate mint" for the flavor. With this code, an entry containing "super chocolate fudge fresh mint" would be included in the results, since it has "chocolate" first and "mint" afterward. Normally, only entries that have the words "chocolate mint" together would be included.
As a note, entries that have "mint chocolate" in them would not be included in the search results. The words in the entry need to be in the same order as the words in the filter.
add_filter( 'frm_where_filter', 'frm_flexible_filter', 10, 2 );
function frm_flexible_filter( $where, $args ) {
$view_id = 186;// Replace with your View ID
$field = 644;// Replace with ID of your field
if ( $args['display']->ID == $view_id && $args['where_opt'] == $field ) {
$search_term = 'color';// Replace with the name of your search param
$search_val = isset( $_GET[ $search_term ] ) ? $_GET[ $search_term ] : '';
if ( $search_val ) {
$search_val_flexible = str_replace( ' ', '%', $search_val );
$where = "( ( fi.id = " . $field . ") AND ( meta_value like '%" . $search_val_flexible . "%' ))";
}
}
return $where;
}
Usage notes:
- Change the $view_id to the id of your View. You can find this in the URL bar when you're editing a View. It's the number following post=.
- Change $field to the id of the field in your filter.
- Change $search_term to the name of your param.
- Use "like" in your field filter.
- Use [get param=your-param] as the value in your field filter. Replace your-param with the name of your param.
Remove User ID filtering for admins across all Views
If you are currently filtering your View to display only the user's own entries but want administrators to have access to all entries, you can remove the filtering conditionally. Use the following code example to grant admin access to all Views, eliminating the need to create individual snippets for each View.
add_filter('frm_where_filter', 'stop_filter_for_admin', 10, 2);
function stop_filter_for_admin( $where, $args ) {
// Define arrays of View IDs and User ID field IDs
$view_ids = [304, 305, 306]; // Replace with the IDs of the Views you want to apply this to
$user_id_fields = [2076, 2079, 2082]; // Replace with the IDs of the user ID fields you want to apply this to
// Check if the current View ID and User ID field ID are in the arrays
if ( in_array($args['display']->ID, $view_ids) && in_array($args['where_opt'], $user_id_fields) ) {
// Apply the filter only if the current user is an administrator
if ( current_user_can('administrator') ) {
$where = "fi.id='" . esc_sql($args['where_opt']) . "'";
}
}
return $where;
}