在搜索中包括自定义分类术语
-
-
诺克罗斯,您能为Jan提出的答案添加一些反馈吗?您是否正在寻找可以完成此工作的插件?Nocross, can you add some feedback to the answer proposed by Jan? Are you probably looking for a plugin that does the job?
- 0
- 2010-11-06
- hakre
-
我最终放弃了这个计划.由于我创建了3个单独的搜索功能(基于某些领域的不同需求),因此我测试的所有插件都将其破坏了.最后,我告诉客户如果他们希望内容可搜索,请在内容中包含字词.I ended up ditching the plan. Since I had created 3 separate search functions (based on different needs in certain areas), all the plugins I tested broke those. In the end, I told the client to include terms in the content if they wanted it searchable.
- 0
- 2010-11-13
- Norcross
-
6 个回答
- 投票数
-
- 2010-12-15
我也建议使用 Search Everything 插件,但是如果您想使用WP的搜索功能来实现此功能,以下是我在Atom主题中使用的代码:
// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb; if (is_search()) $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')"; return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
它基于标签搜索插件: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23
I would recommend the Search Everything plugin too, but if you want to implement this using WP's search function, here's the code I'm using in my Atom theme:
// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb; if (is_search()) $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')"; return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
It's based on the Tag-Search plugin: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23
-
太好了-如何修改此代码以从搜索中排除分类法ID数组?This is great-- how can this code be modified to exclude an array of taxonomy IDs from the search?
- 1
- 2012-01-06
- HandiworkNYC.com
-
应该注意的是,这些钩子的过滤器回调接受* 2 *参数.第二个都是_WP_Query_实例(通过引用传递).任何对"is_search()"或其他_WP_Query_方法调用的检查("is_search()`is_home()等")都应始终直接在查询实例上调用(例如,假设$ query->is_search()`实例变量的名称在回调签名中为`$ query`),而不是模板函数,后者始终引用主查询,而不是过滤器正在运行的查询.It should be noted that the filter callbacks for these hooks accept *2* arguments; the 2nd for all of them being the _WP_Query_ instance which is passed by reference. Any checks for `is_search()` or other _WP_Query_ method calls (`is_search()` `is_home()` etc.) should always be called directly on the query instance (eg. `$query->is_search()` assuming the name of the instance variable is `$query` in the callback signature) rather than the template function which will always refer to the main query, not the query that the filter is running for.
- 0
- 2014-06-07
- Evan Mattson
-
另外,将原始的公开可用搜索字符串直接注入到SQL查询中可能不是一个好主意... [推荐阅读](http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks)Also, probably not a good idea to inject the raw publicly available search string directly into an SQL query... [recommended reading](http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks)
- 6
- 2014-06-07
- Evan Mattson
-
我只想补充一点,这与WPML存在冲突,因为WPML alredy在连接部分使用了" T",因此使用自定义内容而不是tr,tt和t可以解决此问题.I would just like to add that this has a conflict with WPML because WPML alredy uses 'T' in join part, so using something custom instead of tr, tt and t fixes this problem
- 0
- 2016-02-09
- Bobz
-
@EvanMattson —您在上面评论说"将原始的公开可用搜索字符串直接注入到SQL查询中可能不是一个好主意".如何消除这种风险?我阅读了您提供的链接,但看不到它如何链接到原始答案.非常感谢Em.@EvanMattson — you commented above that it's "probably not a good idea to inject the raw publicly available search string directly into an SQL query." How would go about negating that risk? I read the link you provided but couldn't see how that links to the original answer. Many thanks Em.
- 1
- 2020-04-05
- The Chewy
-
@TheChewy上面的答案显示`get_search_query()`直接在WHERE子句的MySQL语句中使用.此代码容易受到SQL注入攻击的攻击,任何人都可以通过将其传递到搜索字段来在您的站点上执行任意SQL查询.显然,这就是搜索在某种程度上起作用的方式,但是重要的一点是,需要正确准备此输入,以确保将其解释为搜索项而不是SQL语句.这是`$ wpdb->prepare()`方法的用途,在推荐的阅读文章中有介绍@TheChewy the answer above shows `get_search_query()` used directly in the MySQL statement of the WHERE clause. This code is vulnerable to SQL injection attacks, where anyone can execute arbitrary SQL queries on your site by passing them through the search field. Obviously that's how searches work to some degree, but the important point is that this input needs to be prepared properly to ensure it is interpreted as search terms and not SQL statements. This is what the `$wpdb->prepare()` method is for which is described in said recommended reading
- 0
- 2020-04-19
- Evan Mattson
-
@TheChewy,您还可以在文档中找到" wpdb ::prepare()"的一些好示例,网址为:https://developer.wordpress.org/reference/classes/wpdb/prepare/@TheChewy you can also find some good examples in the docs for `wpdb::prepare()` here: https://developer.wordpress.org/reference/classes/wpdb/prepare/
- 0
- 2020-04-19
- Evan Mattson
-
@EvanMattson嗨,埃文,谢谢您的投入.我是php和自定义WP代码的新手,这已经超出了我的范围.如果不太难,您是否能够使用添加的$ wpdb->prepare()复制代码?我认为距离了解这类事情还需要几周的时间.@EvanMattson Hi Evan, thanks for the input. I'm new to both php and custom WP code and that has gone so far over my head. Would you be able to duplicate the code with the `$wpdb->prepare()` added if it isn't too hard? I think it'll be weeks before I get close to understanding that sort of thing.
- 1
- 2020-04-21
- The Chewy
-
- 2010-10-07
这是标准的WordPress搜索吗?因为该似乎没有包含分类法(甚至没有标准,例如类别和标签).该代码在
post_title
和post_content
中进行搜索,但是,如果要包含其他内容,则应加入posts_search
过滤器.Is this the standard WordPress search? Because that doesn't seem to include taxonomies (not even standard, like categories and tags) in the search. The code searches in
post_title
andpost_content
, but if you want to include anything else you should hook into theposts_search
filter. -
- 2013-09-08
我在 https://wordpress.stackexchange.com/a/5404/37612,这很好,但是我在那里发现了一个问题,该问题对我不起作用,我将做一个小修改:
- 如果我搜索了分类法标题中的字符串-效果很好
-
如果分类法具有特殊字符,例如与德国" Umlauts"(ö,ä,ü)一起搜索,使用特殊字符搜索oe,ae和ueinsteda-您需要将搜索添加到分类法的标签中-
OR t.slug LIKE '%".get_search_query()."%'
-
如果您搜索搜索查询和分类过滤器的组合,这也可以正常工作
-
但是问题是,当您尝试仅使用分类过滤器时,如果未搜索到任何文本,则搜索挂钩会将空字符串附加到查询中,因此您会在结果中找到所有帖子,而不是仅过滤分类法中的那些. 一个简单的IF语句即可解决该问题.因此,整个修改后的代码就是这样(对我来说很好用!)
函数custom_search_where($ where){ 全局$ wpdb; 如果(is_search()&&get_search_query()) $ where.=" OR(((t.name LIKE'%".get_search_query()."%'ORt.slug LIKE'%".get_search_query()."%')AND {$ wpdb->posts} .post_status='发布')"; 返回$ where; } 函数custom_search_join($join){ 全局$ wpdb; 如果(is_search()&&get_search_query()) $join.=""左联接{$ wpdb->term_relationships}tr ON {$ wpdb->posts}.ID=tr.object_id内联接{$ wpdb->term_taxonomy}tt上tt.term_taxonomy_id=tr.term_taxonomy_id内联接{ $ wpdb->terms}t ONt.term_id=tt.term_id"; 返回$join; } 函数custom_search_groupby($groupby){ 全局$ wpdb; //我们需要按帖子ID分组 $groupby_id=" {$ wpdb->posts} .ID"; if(!is_search()|| strpos($groupby,$groupby_id)!==false||!get_search_query())返回$groupby; //groupby为空,请使用我们的 if(!strlen(trim($groupby)))返回$groupby_id; //不为空,请追加我们的 返回$groupby.",".$groupby_id; } add_filter('posts_where','custom_search_where'); add_filter('posts_join','custom_search_join'); add_filter('posts_groupby','custom_search_groupby');
I tried the solution of Onetrickpony above https://wordpress.stackexchange.com/a/5404/37612, which is great, but I found one issue there, which did not work for me, and I would make one small modification:
- if I searched for a string in the title of the taxonomy - it works great
if the taxonomy has special characters e.g. with german "Umlauts" (ö,ä,ü) and one searches for oe, ae, ue insteda of using the special char - you need to add the search in the slug of the taxonomy -
OR t.slug LIKE '%".get_search_query()."%'
if you search for a combination of a search query and a taxonomy filter - this also works fine
But the problem is, when you try to use only the taxonomy filter - the search hook append an empty string to the query if no text is searched for, and for that reason you get ALL posts in the result, instead of only those from the filtered taxonomy. A simple IF statement solves the problem. So the whole modified code would be this (works perfectly fine for me!)
function custom_search_where($where){ global $wpdb; if (is_search() && get_search_query()) $where .= "OR ((t.name LIKE '%".get_search_query()."%' OR t.slug LIKE '%".get_search_query()."%') AND {$wpdb->posts}.post_status = 'publish')"; return $where; } function custom_search_join($join){ global $wpdb; if (is_search()&& get_search_query()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function custom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false || !get_search_query()) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','custom_search_where'); add_filter('posts_join', 'custom_search_join'); add_filter('posts_groupby', 'custom_search_groupby');
-
- 2010-11-06
我的信息水平与Jan相同.我知道也可以通过插件扩展搜索范围.
您可能正在寻找搜索所有内容(WordPress插件).根据功能列表,它现在支持自定义分类法.
I have the same level of information like Jan. I know it's possible to extend search with plugins as well.
Probably Search Everything (Wordpress Plugin) is what you are looking for. According to the feature list, it now supports custom taxonomies.
-
+1搜索所有插件.它可以按预期工作,并且比标准的Wordpress搜索返回更多结果.+1 For Search Everything plugin. It works as expected and returns more results than standard Wordpress search.
- 0
- 2010-12-02
- PNMG
-
- 2012-12-08
我在WooCommerce购物车插件上遇到了同样的问题.我的搜索结果不包含自定义分类术语"product_tag",因为它不是标准的帖子标记.我在此问题的另一个StackOverflow线程中找到了解决方案:
https://stackoverflow.com/questions/13491828/如何修改wordpress-search-so-it-query-分类学术语和类别术语
由tkelly 编写的代码示例在他的示例中替换了
author
一词时对我有用根据我们对购物车插件的需求添加product_tag
.I have the same problem going on with the WooCommerce cart plugin.. My search results are not including the custom taxonomy term, 'product_tag', because it not a standard post tag. I found a solution in this other StackOverflow thread about the matter:
The code example by tkelly worked for me when replacing the term
author
in his example withproduct_tag
as per our needs for the cart plugins. -
- 2015-11-12
我发现onetrickpony的答案很好,但它会将任何搜索都视为一个词,也不会处理用引号引起来的搜索短语.我修改了他的代码(特别是
atom_search_where
函数)来处理这两种情况.这是我修改后的代码:// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb, $wp_query; if (is_search()) { $search_terms = get_query_var( 'search_terms' ); $where .= " OR ("; $i = 0; foreach ($search_terms as $search_term) { $i++; if ($i>1) $where .= " AND"; // --- make this OR if you prefer not requiring all search terms to match taxonomies $where .= " (t.name LIKE '%".$search_term."%')"; } $where .= " AND {$wpdb->posts}.post_status = 'publish')"; } return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
I found the answer from onetrickpony to be great but it treats any search as a single term and also won't deal with a search phrase enclosed with quotation marks. I modified his code (specifically, the
atom_search_where
function) a bit to deal with these two situations. Here is my modified version of his code:// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb, $wp_query; if (is_search()) { $search_terms = get_query_var( 'search_terms' ); $where .= " OR ("; $i = 0; foreach ($search_terms as $search_term) { $i++; if ($i>1) $where .= " AND"; // --- make this OR if you prefer not requiring all search terms to match taxonomies $where .= " (t.name LIKE '%".$search_term."%')"; } $where .= " AND {$wpdb->posts}.post_status = 'publish')"; } return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
我对两个自定义帖子类型应用了两个自定义分类法.边栏上的术语列表就可以了,它将列出与之关联的所有帖子.但是,如果您搜索特定的术语之一,则不会显示该术语的帖子.
示例: http://dev.andrewnorcross.com/das/all-case-studies/一个> 搜索术语" PQRI"
我什么也没得到.有任何想法吗?我尝试使用各种搜索插件,但是它们要么破坏了我的自定义搜索参数,要么根本无法正常工作.