Formidable Forms

The Most Advanced WordPress Forms Plugin and Form Builder

  • Features
  • Pricing
  • Blog
  • Support
  • Login
  • Get Formidable Forms

frm_where_filter

Last Updated: April 30, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Display Data → frm_where_filter
Heads up!
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.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

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

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;
}
  • Usage
  • Parameters
  • Examples
    • Stop user ID filter for admins
    • Filter by User ID in Dynamic field
    • Check if one of two fields contains a value
    • Add two filters combined with OR
    • Filter a field for one of two values
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms
Install Formidable Lite

Using WordPress and want to get Formidable Forms for free?

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact

Top Features

  • Front End Editor
  • Repeating Fields
  • Views from Entries
  • Calculator Forms
  • Conditional Logic
  • Visual Form Styler
  • Form Templates
  • User Submitted Posts
  • File Upload Forms
  • Spam Protection
  • Multi Page Forms
  • Surveys & Polls
  • Form Graphs & Charts
  • Save and Continue
  • Mobile Forms
  • Stripe Forms
  • PayPal Forms
  • WooCommerce Forms
  • MailChimp Forms
  • User Registration
  • Signature Forms
  • Bootstrap Forms
  • Quiz Maker
  • Zapier Forms
  • Salesforce Forms
  • HubSpot Forms
  • API Webhooks
  • Multilingual Forms
  • Directories

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • Blog

Copyright © 2021 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews