Reply To: Search matching only ends of strings

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Search matching only ends of strings Reply To: Search matching only ends of strings

#6658

Ernest Marcinko
Keymaster

Hi!

I'm guessing this is stored in a custom field?

The only possible explanation I can see in this case is that there are lots of posts ending “123” and then beginning “123”. There is no difference searching 3 or 4 digits on database level, so if it works on 4 digits, it must work on 3 as well.
The case is probably that for example the limit is set to 10 posts. The plugin passes the query to the database and it returns the first 10 matching results with the highest relevance. If the first 10 matching contain 123 either on the beginning, the end or in the middle it's immediately returned. In this case the relevance is the same for any case.

I think I might have a solution though 🙂 If you are indeed using custom fields, then open up the wp-content/plugins/ajax-search-pro/includes/search/search_content.class.php file and go to line 813 and 824, the two lines should be identical, something like:

$parts[] = "( $wpdb->postmeta.meta_key='$cfield' AND …

Then, add this line just below both of those two lines:

$relevance_parts[] = "(case when ($wpdb->postmeta.meta_key='$cfield' AND " . $pre_field . $wpdb->postmeta . ".meta_value" . $suf_field . " LIKE $pre_like'" . $_like . "%'$suf_like) then 50 else 0 end)";

This will increase the relevance in case the search string is found on the beginning of the word by 50, which ensures that those results are displayed first. I just tested this, it seems to be working as expected.

Let me know if anything changes!

Best,
Ernest Marcinko

If you like my products, don't forget to rate them on codecanyon :)