Skip Navigation

[Resolved] Customize the Query by Text to search only by certain characters.

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

Problem:
I have values stored in a Post Title or Body as the example "AE 2345"
I want, that when a Search by number is performed, it queries only the Number in that stored value.
It means, when I search by 2345 the post AE 2345 should be found.

Solution:
You need Custom PHP code for this.
An example is to see here:
https://toolset.com/forums/topic/remove-spaces-and-ignore-case-2/#post-418246

Relevant Documentation:
http://www.php.net/manual/en/function.preg-replace.php
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_custom_field_filter_processed_value/

This support ticket is created 7 years, 9 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by zoeS 7 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#418040

Hello, I had help from Shane a while ago with a problem (https://toolset.com/forums/topic/remove-spaces-and-ignore-case/ ) but we were unable to resolve it as the site was still on my local computer at the time. It is now online and I would still like to know how to make the search filter insensitive to spaces.

The site is at hidden link
At the top of the page is a "reference" search box. A real example of a reference is MV 32448, but I would like to be able to enter MV32448, without the space.

My functions.php file contains the following, but it is not working:

add_filter( 'wpv_filter_query', 'custom_search_query', 10, 3 );
  
function custom_search_query( $query, $settings, $view_id ) {
    if ( isset($_GET['wpcf-reference'])) {
        $lowercase = str_replace(' ', '', $_GET['wpcf-reference']);
       $query = array(
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key'     => 'wpcf-reference',
                    'value'   => $_GET['wpcf-reference'],
                    'compare' => '=',
                ),
                array(
                    'key'     => 'wpcf-reference',
                    'value'   => $lowercase,
                    'compare' => '=',
                ),
            ),
        );
    }
    return $query;
}

Many thanks

#418052

In your old ticket, you wanted:
"So if the reference for the property is entered in the database as AB 1234, but the user enters ab1234, it should still be a match."

This is absolutely possible without any Custom Code.

1. Insert your Custom Field Search to Views Search HTML editor
2. Choose "LIKE" in the comparison operator

Now if you search lower or upper case, it will return the result
Keep in mind that any query that is not EQUAL TO, of course, could also return some results that do not exactly match, but this is just what we have at disposal.

In your new request, you want to eliminate the empty space.

It is confusing.
See:
You store a value, that HAS an empty space.
You search a value, that HAS empty space ==> all good, it will return results.

But now, if you search for things with NO empty space, you search for a whole new value.

You can SUBTRACT as many characters you want, that will work, but ADDING characters will search fo new values.

That is why a LIKE query for "value" will work but a query for "values" will not.
(given the stored value is "value m)

So what you actually need is a code that CONVERTS search terms holding NO white space, to terms holding one (since your stored data definitely has an empty value)

That is not possible, for logical reasons. Where should the code add the empty space in your string?

That would mean you need to get the value of the stored field first, strip the empty field and compare it to your search term.

I do not see an easy way to do this.

I would need attention from the Developers in this, but I can tell that this is not easily possible and it would fall in Custom Code as well.

Let me know if the LIKE search as elaborated above is enough for your project

#418065

Yes! Thank you Beda I understand better now, and I think I have a solution. The references are composed of 2 letters, plus some numbers. I believe the numbers are in fact unique, and the letters are just there to indicate the type of property. So is there a simple way to strip all but numeric characters from the search term and then use a "like" operator to locate the correct property. Even if it returned a couple of properties (because I'm wrong and the number is not unique), it would still be acceptable I think. Thanks again!

#418246

You ask me if there is an easy way, and the answer is no.

It requires elaborated Custom Code.
You can not use the Views Custom Field native Search for this.

You will need to write a Custom Filter that filters the View, and it requires to strip as you say the not numerical characters.

I am wondering, how are your Visitors to guesss those numbers?
Would it not be much better give those Entries a more "intuitive" meaning/value?
Even with numerical values, all you would need to do is advise the user to search by Number, not by letter.
I mena, it will need some sort of "notice" to the user anyway, otherwise, a common user, will search for "home xy" and not for "MA 2244"
So you could as well advise to searhc for numerical value only, correct?

I am not neglecting to help with this code, but I try to make things simpler for you.
And that Custom Code will require some work, additionally it won't make your Search much more intuitive.

You could use the preg_replace PHP native function to strip all non numerical characters:
hidden link

Then you could filter the value of the field coming from the View settings after passing through the check for URL paramameter:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_custom_field_filter_processed_value/

This would give a code similar to this:

add_filter( 'wpv_filter_custom_field_filter_processed_value', 'prefix_next_month_filter', 10, 3 );
 
function prefix_next_month_filter( $value, $key, $view_id ) {
    if ( $view_id == 48 && $key == 'wpcf-field' ) { // if we are displaying our View with ID 25 and the filter is for the field with name wpcf-post-date-filter
	$value = preg_replace('/\D/', '', $value );
    }
    return $value;
}

Pleaes change the View ID and Field Name.
Then set your View Parametric Search to find by "LIKE".

#418380

Thank you, thank you so much! That function was exactly what I needed. I had got as far as the preg_replace bit, but just haven't the experience to construct the whole function. It works perfectly now.

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