Skip Navigation

[Resolved] Get parametric search values to find text WITHIN, not EQUAL TO a custom field

This thread is resolved. Here is a description of the problem and solution.

Problem:
Parametric search with checkbox field with OR condition

Solution:
You can use views 'wpv_filter_query' hook to modify the view query on fly and establish OR condition between checkbox field using some custom code.

You can find the proposed solution with the following link:
https://toolset.com/forums/topic/parametric-search-keeps-displaying-wrong-results/#post-357800

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

This support ticket is created 8 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 17 replies, has 2 voices.

Last updated by simonF-5 8 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#356650

I am trying to set up a parametric search that will allow me to:
- Do a keyword search on all posts
- Filter the results by category (using dropdown selection)
- and/or filter the results by the content of one custom field (using checkboxes to allow for checking multiple options)
- and/or filter the results by the content of another custom field (using checkboxes to allow for checking multiple options)

The custom fields are not numeric, they are text (one has values: Easy,Intermediate,Advanced, the other: Introductory,Overview,Further,Specialised).

I can get this to work using IN as comparison function. But the problem is that some of my custom fields the values are combined (e.g. the value in one says 'easy to intermediate'). I'd like this to also be picked up when filtering (ideally by both 'easy' and 'intermediate' options).

So, is there a way to set the filter up so that the determined values are not EQUAL to the value in the custom field, but are CONTAINED in the value in the custom field? E.g. so that when I check 'Easy', it will find things with field that says 'Easy', but also 'Easy to intermediate', 'Pretty easy', etc.

Here is the URL I am trying to get this on: hidden link

All help really appreciated!

#356802

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

1)
I hope you have read the following documentation.
=> https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

2)
If #1 does not help:
You can also use "wpv_filter_query" to change the query on fly.
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

Could you please try above options and let me know if you still need further assistance.

#356812
Capture1.JPG
Capture.JPG

Hi, thanks for a quick reply!

I'm sorry, I wasn't clear in my post. I don't want to create a view where I can display filtered results to the users. I want to create a view which will allow the users to search for posts on the site and filter the results by the content of custom fields. I used this guide to work my way into it: https://toolset.com/documentation/user-guides/front-page-filters/

I want to create an 'advanced search' page for my site, with filtering options as shown on the attached picture (or under the address I pasted above). To create those filters, I created a parametric search view and added them as on the other attached picture. But while the view has no problem filtering by the value 'Specialised reading' (see pic), it fails to find anything when searching for 'urther', while I would like it to be able to pick up all posts which have a value 'Further reading' in the relevant field. The reason why I want that is because some posts will have a value 'Specialised or further reading', or something yet different containing the word 'Further'.

Is there a way to do this?

#357108

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

By reviewing screenshot you shared with us - what if you should try to set comparison function from "IN" to to "LIKE".

Could you please try to check following documentation that may help you:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

#357134
Capture.JPG

Thanks Minesh,
As far as I can see, there is no 'LIKE' function for the checkboxes input kind. See pic below. The functions don't come up at all in the code displayed on the view page, so I can't change it manually there either. Is there some other way to add it?

#357178

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Actually checkboxes type uses serialized array to store data to database. That is the reason option "Like" is not available. If you will change your field type to select then "LiKE" option will be available.

So for now, with checkboxes "Like" is not possible but you may try with "wpv_filter_query" and change he query at run time that may work.
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

#357278

Thanks!

The problem is, I do want the users to be able to select several options (e.g. search for both easy and intermediate texts) and the 'select' type only allows you to select one value per filter. Is there any other way to get the desired functionality? Maybe at least some way to put an OR between two 'select' filters?

I don't understand what you meant by trying "wpv_filter_query". I read through the documentation under the link you sent, but I find it quite unclear or I'm just not skilled enough to know what to do with it. Can you explain in more basic terms please?

Alternatively, if it's not possible to do what I need with Views, can you recommend a search plugin which will:
- search and filter by custom types fields
- have the LIKE function for checkboxes type
- display search results in the same way they are displayed using Views

#357442

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately I do not have any other plugin to recommend you that work like views.

Could you please change your search field to checkboxes and send me temporary access details and I'll try if I can fix your issue.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#357769

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now I can see that when I check the checkbox "Further" it has the first result which have "Specialised or further reading".

#357777
Capture.JPG

It does, thanks! But when I check both 'Further' and 'Specialised', it doesn't return anything at all. I'd like it to return a list of all entries which have either 'further' or 'specialised' (or both) in the field. That's the whole point of having checkboxes rather than select or radio buttons.

#357800

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've added following code to your functions.php file.

add_filter( 'wpv_filter_query', 'custom_search_criteria',10,2 );
function custom_search_criteria( $query_args ,$view_settings ) {

if (isset($view_settings['view_id']) && $view_settings['view_id'] == 1283 ) {
	
	foreach((array)$query_args['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpcf-use'){
							
				$values = explode(",",$v['value']);
				if(count($values) > 1){
					unset($query_args['meta_query'][$k]);
					
					foreach($values as $x=>$y):
						$query_args['meta_query'][$k][$x]['key'] = 'wpcf-use';
						$query_args['meta_query'][$k][$x]['type'] = 'CHAR';
						$query_args['meta_query'][$k][$x]['compare'] = 'LIKE';
						$query_args['meta_query'][$k][$x]['value'] = $y;
					endforeach;
					$query_args['meta_query'][$k]['relation'] = 'OR';
					
					
				}
			}
        endforeach;
		
}

return $query_args;
}

#357855

This works a dream, thank you!

I tried to paste the same code to the functions.php on the live website and that doesn't work, though. Can I just confirm that's the file that's in the theme folder? Is there a specific place in it where this should be pasted?

Meanwhile - would you be able to help with the alphabetical display of content archive views as well? (mentioned in the previous post) It's ok if not, but please let me know.

#357860

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that your original issue is resolved.

May I kindly ask you please to open a new ticket for your additional question? This will help other users with similar problems to find solutions when searching the forum.

Thank you for understanding.

#357883

Sure!
Can you please first explain how to make this work on the live website as well? It only works on the test site. I tried to add the code to functions.php in my theme, and to copy the entire functions.php from the test installation to the live one, but it still doesn't want to work on the live site.

#357886

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

if (isset($view_settings['view_id']) && $view_settings['view_id'] == 1283 ) {

In your code could you please try to replace "1283" with your view ID. Hope this will fix issue with your live site.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.