frm_where_filter

Back to Top
Knowledge BaseDisplay Data → frm_where_filter

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);

Parameters

  • $where (array)
  • $args (array)
    • 'where_opt' (string)
    • 'where_is' (string)
    • 'where_val' (string)
    • 'form_id' (integer)

Examples

Formidable Hook - Helpful

Stop user ID filter for admins

Submitted by  — 8 years ago

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;
}
Formidable Hook - Helpful

Filter by User ID in Dynamic field

Submitted by  — 8 years ago

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) and 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'] ."'";
	else if(is_array($entry_id))
		$where = "meta_value in (". implode(',', $entry_id) .") and fi.id='". $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;
}
Formidable Hook - Helpful

Check if one of two fields contains a value

Submitted by  — 8 years ago

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 and change 100 to the ID of the field you have added as a filter
        $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;
}
Formidable Hook - Helpful

Add two filters combined with OR

Submitted by  — 8 years ago

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;
}
If you would like to add your own example, click here.

Have something to add?

Click here to provide feedback on this page.