简体   繁体   English

在WordPress中搜索多个术语

[英]Searching for Plural Terms in WordPress

Does anyone know how to make the Wordpress system adapt to plural searches? 有谁知道如何使Wordpress系统适应复数搜索?

IE, if a user searches for "Responsive Themes", it will also search for "Responsive Theme" IE,如果用户搜索“响应主题”,则还将搜索“响应主题”

Here's a good answer . 这是一个很好的答案 It recommends using the inflector class in wp-dev-library to create a custom function that will singularize plural words to do just what you are describing. 它建议使用wp-dev-libraryinflector类创建一个自定义函数,该函数将复数形式的单词单数化以完成您所描述的内容。

I'm not a wordpress expert but I can recommend a simple shortcut that covers words whose plurals end in "s". 我不是wordpress专家,但我可以推荐一个简单的快捷方式,该快捷方式涵盖复数以“ s”结尾的单词。

Take the incoming search term(s), and strip the final "s" from any word. 接受输入的搜索词,并从任何单词中去除最后的“ s”。

I suppose wordpress provides you the search query as an array of terms? 我想wordpress为您提供搜索查询作为一组术语? If not, you can create one like this: 如果没有,则可以这样创建一个:

$query_string = 'Dogs and cats';

$search_terms = array_filter(explode(' ',$query_string)); // explode creates an array, filter removes any empty elements.

$search_terms_singular = array_map('rtrim',$search_terms,array_fill(0,sizeof($search_terms),'s')); // trim "s" from the end of every word.

Now search your db using the singular terms. 现在,使用单数术语搜索数据库。 Plural terms should also match if you are using SQL LIKE "%term%" or LIKE "term%" , (and I think for FULLTEXT also). 如果您使用的是SQL LIKE "%term%"LIKE "term%" ,则多个术语也应匹配(并且我也认为FULLTEXT也适用)。

If you need to make the singular search back into a string: 如果需要将单个搜索重新搜索为字符串:

$singular_query_string = implode(' ',$search_terms_singular);

Again, this is a quick and dirty solution that only affects words ending in "s", but I have used similar code in Magento and found it to be quite effective. 同样,这是一种快速而肮脏的解决方案,仅影响以“ s”结尾的单词,但是我在Magento中使用了类似的代码,并发现它非常有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM