Filter view by contains (filter by multiple checkbox options)

By: Tom Boltwood | Asked: 01/27/2023
ForumsCategory: General questionsFilter view by contains (filter by multiple checkbox options)
Tom BoltwoodTom Boltwood asked 1 year ago

I have a list of entries where one of the columns can contain multiple entries. If I select multiple species from a checkbox field, the filter doesn't work. I had assumed there might be a filter like 'contains' instead of like, but there doesn't seem to be. Any ideas on how I might achieve this?

2 Answers
Best Answer
Tom BoltwoodTom Boltwood answered 1 year ago

Have adapted the code on the formidable site to do what I want, posting it here in case it helps anyone. frm_search_multiple_vals( $where, $args ) {
$view_id = 472; // Replace with your View ID
$field = 15; // Replace with ID of your field
if ( !empty( $_GET['species'] ) ) {
$search = explode(', ', $_GET['species']);
if ( $args['display']->ID == $view_id && $args['where_opt'] == $field ) {
$where = "( ( = " . $field . ")";
foreach ( $search as $term ) {
$where .= " AND ( ( meta_value like '%" . $term . "%' ) )"; } $where .= " )";
return $where; } } return $where;}add_filter( 'frm_where_filter', 'frm_search_multiple_vals', 10, 2 );

Tom BoltwoodTom Boltwood replied 1 year ago

Something seriously wrong with the code formatting on this forum. Edited a number of times to make it look okay, but given up.

Victor Font answered 1 year ago
MySQL doesn't have a contains function. MySQL uses like (same as contains) or locate(). Have you examined the way Formidable stores multiple checkboxes in the database? When you have a multiple selection checkbox field, Formidable stores the values as a serialized array. This is an example of checkbox data as stored in the database where two or more selections are made: a:2:{i:0;s:8:"Option 1";i:1;s:8:"Option 2";}. Even if one checkbox is chosen, the value is stored in a serialized array. When the form's data is sent to the server, the PHP $_POST associative array also stores checkbox values as an array. Since a serialized array stores values in a specific order, you will never find a match for multiple checkboxes unless you manually process the data with custom code and search for each array element as an individual value in the query's where clause. (i.e., field like %value1% OR field like %value2% OR field like %value3%) You can do the manual processing to change the where clause in frm_where_filter, or you can completely modify the generated SQL in the frm_view_order filter.
Tom BoltwoodTom Boltwood replied 1 year ago

thanks, I'm currently having a look at although now run into other, unrelated problems. I hate working on sites hosted by other people…

Victor Font replied 1 year ago

LOL! There's nothing better than adjusting your own well-commented code and working with your own libraries! I stopped being shocked at some of the code I've inherited when taking on new clients. Sometimes the logic is baffling or simple functions are far too over engineered.

My background is enterprise development. I'm talking about very big systems where we worked in globally distributed dev teams of 50 or more, sometimes jumping into each other's code to help out. Code can never be over commented. The more details we leave behind in our code for other developers to follow, the better. This is especially true in the opensource world. I guess my biggest peeve when I take over a site is the code not being commented and sometimes the best approach is starting over and do it right.

I don't know if you like to watch HGTV, but there's a new show on called "Rico to the Rescue" that I find entertaining. I feel like that guy with some of the sites I've seen!

Tom BoltwoodTom Boltwood replied 1 year ago

Haha, I'm that guy, I think you'd hate to work on some of my sites, I just want to get it done! My current issue is no access to the server. Annoying as it's refusing to upload CSV files and I can't seem to find a reason for it.

Victor Font replied 1 year ago

Sounds like mime-type.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community