Skip Navigation

[Geschlossen] remove spaces and ignore case

This support ticket is created vor 7 Jahre, 12 Monate. 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 Antworten, has 2 Stimmen.

Last updated by Shane vor 7 Jahre, 11 Monate.

Assisted by: Shane.

Author
Artikel
#393926

I am trying to add a "search by reference" box in a site using the Real Estate reference site. I have two questions: is it possible to go straight to the details of the property in question (assuming no duplicate references) and how can I get it to ignore case and any spaces, both in the database and in the user input? 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.

#394372

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zoe,

Thank you for contacting our support forum.

Let me see how best I can answer this for you 🙂

1. For this one it would need some custom coding in order to redirect directly to the post if there are no other entries found.

2. This one you should be able to achieve with the addition of a custom filter, where you can manipulate the search value and query the database for both formats.

Could you let me know what exactly you are going to query, the Post title or a custom field value.

Thanks,
Shane

#394492

Thank you Shane, the property reference is a custom field called "reference".

#395055

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zoe,

Thank you for the continued patience, I'm currently seeing if I can work on a solution for you with this.

It may take some time.

Thanks,
Shane

#395400

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zoe,

Please try this in your functions.php file

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

What you need to do is to replace the 'wpv_post_search' with the custom field slug and the key value price and color with the custom field slug as well.

This should allow you to return values for both instances.

Please let me know if this helps.
Thanks,
Shane

#395427

Thank you so much Shane for taking so much time over this. It's not working yet, but it is having some affect as the search no longer works even with the exact reference used. Here is my code. Have I missed something?

function custom_search_query( $query, $settings, $view_id ) {
    if ( isset($_GET['reference'])) {
        $lowercase = str_replace(' ', '', $_GET['reference']);
       $query = array(
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key'     => 'reference',
                    'value'   => $_GET['reference'],
                    'compare' => '=',
                ),
                array(
                    'key'     => 'reference',
                    'value'   => $lowercase,
                    'compare' => '=',
                ),
            ),
        );
    }
    return $query;
}
#395480

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zoe,

Since the fields are types custom fields then they will have the name.

wpcf-reference

So you need to use wpcf-reference instead of just reference. If this still does not work, please allow me to have a link to your search so I can have a look.

Thanks,
Shane

#395648

Hi Shane

Sorry you're right, I corrected those and now at least the search works as it used to, which is to return the correct property as long as the space is entered correctly. Unfortunately, it still doesn't work without spaces though. The site is still just on my Mac using MAMP so I can't give you access. I have tried to recreate it on a Discover-WP site, but I can't modify the themes or upload my own.

I'm afraid my PHP really isn't very good at all, but looking at that code, is the string_replace function not operating only on the user input, whereas the problem is that the spaces exist in the database, and the user input may not contain them? So before comparing the two, string_replace should be applied to both the user input and the actual field content?

Maybe i will have to pick this up again a bit further down the line when the site is on th server. Thanks very much for your help.

Zoë

#395839

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zoe,

I added the str_replace function to remove the spaces from one values and use it in the OR case.

So one case takes in the original and another takes in the case where the spaces have been stripped.

In essence it should match both cases with and without spaces.

However once you get this setup on a live server we can do some more debugging to get it working as intended.

Thanks,
Shane

Das Thema „[Geschlossen] remove spaces and ignore case“ ist für neue Antworten geschlossen.