Custom Search | Wrong output & question

== Short answer ==

If you use the found $1 you should use LIKE and not = as you match against the title’s LIKE. So replace

((abc18meta.meta_key = 'plz') AND (abc18meta.meta_value = $1))

into

((abc18meta.meta_key = 'plz') AND (abc18meta.meta_value LIKE $1))

You could change your preg_replace search by ignore the wrapping (see below) of **ABC* but then you should take care for SQL-injection yourself!

== Long answer ==

Say we are searching for ABC.

Your preg_replace is searching for the prepares $where

"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/"

but what it finds for $1 in there is a wrapped **ABC*

{f5fd7...deb8c1b}ABC{f5fd7...deb8c1b}

which you then use in your SQL

((abc18meta.meta_key = 'plz') AND (abc18meta.meta_value = $1))
                    OR ((abc18meta.meta_key = 'ort') AND 
(abc18meta.meta_value = $1))
                    OR ((abc18meta.meta_key = 'bundesland') AND 
(abc18meta.meta_value = $1))
                    OR ((abc18meta.meta_key = 'regionaler_zusatz') AND 
(abc18meta.meta_value = $1))

This wrapped ABC value of {f5fd7...deb8c1b}ABC{f5fd7...deb8c1b} is later ‘expanded’ into %ABC%

//wp-includes/wp-db.php:1789
$query = apply_filters( 'query', $query );

Leave a Comment