带有“ post_title LIKE'something%'”的WP_Query?
5 个回答
- 投票数
-
- 2011-05-30
我可以通过
WP_Query
上的过滤器解决此问题.可以检测到一个额外的查询变量并将其用作标题的前缀.add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 ); function wpse18703_posts_where( $where, &$wp_query ) { global $wpdb; if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $wpse18703_title ) ) . '%\''; } return $where; }
这样,您仍然可以调用
WP_Query
,只需将标题作为wpse18703_title
参数传递(或将名称更改为更短的名称即可).I would solve this with a filter on
WP_Query
. One that detects an extra query variable and uses that as the prefix of the title.add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 ); function wpse18703_posts_where( $where, &$wp_query ) { global $wpdb; if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $wpse18703_title ) ) . '%\''; } return $where; }
This way you can still call
WP_Query
, you just pass the title as thewpse18703_title
argument (or change the name to something shorter).-
这个以某种方式缺少`$ wpdb->prepare()`.This one is somehow missing the `$wpdb->prepare()`.
- 1
- 2012-11-06
- kaiser
-
@kaiser:已经很长时间了,但是我认为这对于`prepare()`是不可能的.$ wpdb->prepare('LIKE"%s %%"','banana')`将返回`" LIKE'banana'%'"``,因此我们必须自己构造查询,并进行转义.@kaiser: It's been a long time, but I think this was not possible with `prepare()`. `$wpdb->prepare('LIKE "%s%%"', 'banana')` would return `"LIKE ''banana'%'"`, so we have to construct the query ourselves, and do the escaping too.
- 0
- 2012-11-06
- Jan Fabry
-
@JanFabry很高兴见到你agaaaaaaaain!:)聊天吧,嗯?StopPress很高兴见到您.关于`prepare()`.是的,这很棘手,在解决之前,我不得不尝试几次.从我刚做的事情中:`$ wpdb->prepare('AND {$ wpdb->posts} .post_title像%s',esc_sql('%'.like_escape(trim($term)).'%'))).而且我很确定`esc_sql()`是不必要的,只是偏执.@JanFabry Happy to see you agaaaaaaaain! :) Drop by in chat some time, hm? StopPress would be happy to see you. About that `prepare()`. Yeah, that's tricky and I had to try that several times, before I got around it. From something I just made: `$wpdb->prepare( ' AND {$wpdb->posts}.post_title LIKE %s ', esc_sql( '%'.like_escape( trim( $term ) ).'%' ) )`. And I'm pretty sure the `esc_sql()` is unnecessary and just paranoid.
- 1
- 2012-11-07
- kaiser
-
看来您无法搜索带有''`(撇号)的字符串.我想是因为逃跑了吗?我尚未找到解决方案It seems that you can't search a string with `'` (apostrophe) inside. I guess it's because of escaping ? I didn't find the solution yet
- 0
- 2018-08-30
- Vincent Decaux
-
- 2013-04-19
简体:
function title_filter( $where, &$wp_query ) { global $wpdb; // 2. pull the custom query in here: if ( $search_term = $wp_query->get( 'search_prod_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\''; } return $where; } $args = array( 'post_type' => 'product', 'posts_per_page' => $page_size, 'paged' => $page, // 1. define a custom query var here to pass your term through: 'search_prod_title' => $search_term, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ); add_filter( 'posts_where', 'title_filter', 10, 2 ); $wp_query = new WP_Query($args); remove_filter( 'posts_where', 'title_filter', 10 ); return $wp_query;
Simplified:
function title_filter( $where, &$wp_query ) { global $wpdb; // 2. pull the custom query in here: if ( $search_term = $wp_query->get( 'search_prod_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\''; } return $where; } $args = array( 'post_type' => 'product', 'posts_per_page' => $page_size, 'paged' => $page, // 1. define a custom query var here to pass your term through: 'search_prod_title' => $search_term, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ); add_filter( 'posts_where', 'title_filter', 10, 2 ); $wp_query = new WP_Query($args); remove_filter( 'posts_where', 'title_filter', 10 ); return $wp_query;
-
请在代码中附上说明.Please include an explanation along with your code.
- 13
- 2013-04-19
- s_ha_dum
-
大大简化Great simplification
- 2
- 2014-11-10
- Timo Huovinen
-
我认为代码是自我解释的,对我来说至少.感谢您分享完整的脚本.Code is i think self explained, atleast for me. Thanks for sharing complete script.
- 2
- 2017-08-08
- Hassan Dad Khan
-
使用'$ wpdb->esc_like('代替'esc_sql(like_escape('Use '$wpdb->esc_like (' instead of 'esc_sql( like_escape('
- 2
- 2018-02-19
- fdrv
-
@fdrv你是正确的,但是根据wp文档$ wpdb->esc_like仍然需要esc_sql().所以我认为正确的代码应该是esc_sql($ wpdb->esc_like($ search_term))@fdrv You are right but according to wp docs $wpdb->esc_like still need esc_sql(). So I think the correct code would be esc_sql( $wpdb->esc_like( $search_term ) )
- 0
- 2019-10-18
- Waqas Bukhary
-
不推荐使用like_escape 4.0.0使用wpdb ::esc_like`like_escape` deprecated 4.0.0 Use `wpdb::esc_like`
- 0
- 2020-03-14
- Empty Brain
-
`remove_filter`仅使用3个参数,最后一个可以删除.`remove_filter` only uses 3 arguments, the last one can be removed.
- 2
- 2020-06-24
- Skatox
-
- 2015-01-02
想更新此代码,因为esc_sql()在4.0或更高版本中已被弃用,因此您在wordpress 4.0及更高版本上使用的代码都将被删除.
function title_filter($where, &$wp_query){ global $wpdb; if($search_term = $wp_query->get( 'search_prod_title' )){ /*using the esc_like() in here instead of other esc_sql()*/ $search_term = $wpdb->esc_like($search_term); $search_term = ' \'%' . $search_term . '%\''; $where .= ' AND ' . $wpdb->posts . '.post_title LIKE '.$search_term; } return $where; }
其他东西都是一样的.
我还要指出,您可以在WP_Query参数中使用 s 变量来传递搜索词,我相信这些词也会搜索帖子标题.
赞:
$args = array( 'post_type' => 'post', 's' => $search_term, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ); $wp_query = new WP_Query($args);
Wanted to update this code you guys worked on for the wordpress 4.0 and above as esc_sql() is deprecated in 4.0 higher.
function title_filter($where, &$wp_query){ global $wpdb; if($search_term = $wp_query->get( 'search_prod_title' )){ /*using the esc_like() in here instead of other esc_sql()*/ $search_term = $wpdb->esc_like($search_term); $search_term = ' \'%' . $search_term . '%\''; $where .= ' AND ' . $wpdb->posts . '.post_title LIKE '.$search_term; } return $where; }
Rest of the stuff is same.
Also I want to point out you can use s variable within WP_Query arguments to pass search terms, which will also search for post title i believe.
Like this:
$args = array( 'post_type' => 'post', 's' => $search_term, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ); $wp_query = new WP_Query($args);
-
" search_prod_title"到底是什么?我应该将其更改为其他内容吗?What exactly `search_prod_title` is? Should i change this to something else?
- 0
- 2017-03-24
- Antonios Tsimourtos
-
从什么时候开始使用`esc_sql`?不是.$ wpdb->escape是... https://developer.wordpress.org/reference/functions/esc_sql/Since when is `esc_sql` depricated? It's not. `$wpdb->escape` is though... https://developer.wordpress.org/reference/functions/esc_sql/
- 0
- 2018-02-02
- Jeremy
-
请注意,s参数也搜索帖子内容,这可能不是期望的目标.=)Note that the s parameter searches the post content as well, which may not be the desired aim. =)
- 1
- 2018-04-19
- Christine Cooper
-
不喜欢like_escape` 4.0.0使用wpdb ::esc_like`like_escape` deprecated `4.0.0` Use `wpdb::esc_like`
- 0
- 2020-03-14
- Empty Brain
-
- 2018-04-19
在这里发布了一些易受攻击的解决方案后,我提供了一个经过简化和消毒的版本.
首先,我们为
posts_where
过滤器创建一个函数,该函数仅允许您显示符合特定条件的帖子:function cc_post_title_filter($where, &$wp_query) { global $wpdb; if ( $search_term = $wp_query->get( 'cc_search_post_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like( $search_term ) . '%\''; } return $where; }
现在,我们将
cc_search_post_title
添加到我们的查询参数中:$args = array( 'cc_search_post_title' => $search_term, // search post title only 'post_status' => 'publish', );
最后将查询过滤器包装起来:
add_filter( 'posts_where', 'cc_post_title_filter', 10, 2 ); $query = new WP_Query($args); remove_filter( 'posts_where', 'cc_post_title_filter', 10 );
使用get_posts()
某些检索帖子的函数不会运行过滤器,因此您附加的posts_where过滤器函数不会修改查询.如果计划使用
get_posts()
查询帖子,则需要在参数数组中将suppress_filters
设置为false:$args = array( 'cc_search_post_title' => $search_term, 'suppress_filters' => FALSE, 'post_status' => 'publish', );
现在您可以使用
get_posts()
:add_filter( 'posts_where', 'cc_post_title_filter', 10, 2 ); $posts = get_posts($args); remove_filter( 'posts_where', 'cc_post_title_filter', 10 );
s
参数如何?s
参数可用:$args = array( 's' => $search_term, );
在将搜索词添加到
s
参数的过程中,它将搜索帖子标题,同时也搜索帖子内容.WP 4.4中添加的
title
参数又如何呢?将搜索词传递到
title
参数:$args = array( 'title' => $search_term, );
是区分大小写的和
LIKE
,而不是%LIKE%
.这意味着搜索hello
不会返回标题为Hello World
或Hello
的帖子.With some vulnerable solution posted here, I come with a bit simplified and sanitized version.
First, we create a function for the
posts_where
filter which allows you to only show posts matching specific conditions:function cc_post_title_filter($where, &$wp_query) { global $wpdb; if ( $search_term = $wp_query->get( 'cc_search_post_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like( $search_term ) . '%\''; } return $where; }
Now we add
cc_search_post_title
into our query arguments:$args = array( 'cc_search_post_title' => $search_term, // search post title only 'post_status' => 'publish', );
And finally wrap the filter around the query:
add_filter( 'posts_where', 'cc_post_title_filter', 10, 2 ); $query = new WP_Query($args); remove_filter( 'posts_where', 'cc_post_title_filter', 10 );
Using get_posts()
Certain functions which retrieve posts do not run filters, so the posts_where filter functions you attach will not modify the query. If you plan to use
get_posts()
to query your posts, you need to setsuppress_filters
to false in your argument array:$args = array( 'cc_search_post_title' => $search_term, 'suppress_filters' => FALSE, 'post_status' => 'publish', );
Now you can use
get_posts()
:add_filter( 'posts_where', 'cc_post_title_filter', 10, 2 ); $posts = get_posts($args); remove_filter( 'posts_where', 'cc_post_title_filter', 10 );
What about the
s
parameter?The
s
parameter is available:$args = array( 's' => $search_term, );
While adding your search term into the
s
parameter work and it will search the post title, it will also search the post content.What about the
title
parameter which was added with WP 4.4?Passing a search term into the
title
parameter:$args = array( 'title' => $search_term, );
Is case sensitive and
LIKE
, not%LIKE%
. This mean search forhello
will not return post with titleHello World
orHello
.-
优秀的.我一直在寻找'post_title'作为参数,显然没有找到任何东西.Excellent. I was looking for 'post_title' as a parameter and, obviously, didn't find anything.
- 0
- 2019-09-13
- MastaBaba
-
我在wp查询或获取帖子时遇到错误:" E_WARNING文件»class-wp-hook.php«在第288行出错:cc_post_title_filter()的参数2应该是参考,给定值I'm getting an error with wp query or get posts: "E_WARNING Error in file »class-wp-hook.php« at line 288: Parameter 2 to cc_post_title_filter() expected to be a reference, value given
- 0
- 2020-03-03
- Elkrat
-
@Elkrat从" cc_post_title_filter"中的"&$ wp_query"中删除"&".@Elkrat Remove the `&` from `&$wp_query` in `cc_post_title_filter`.
- 0
- 2020-05-05
- Mattimator
-
- 2015-04-28
以我之前的其他答案为基础,为了在您要搜索包含元字段或帖子标题中包含单词的帖子的情况下提供灵活性,我通过参数"title_filter_relation"给出该选项.在此实现中,我只允许使用默认值为" AND"的" OR"或" AND"输入.
function title_filter($where, &$wp_query){ global $wpdb; if($search_term = $wp_query->get( 'title_filter' )){ $search_term = $wpdb->esc_like($search_term); //instead of esc_sql() $search_term = ' \'%' . $search_term . '%\''; $title_filter_relation = (strtoupper($wp_query->get( 'title_filter_relation'))=='OR' ? 'OR' : 'AND'); $where .= ' '.$title_filter_relation.' ' . $wpdb->posts . '.post_title LIKE '.$search_term; } return $where; }
下面是一个非常简单的帖子类型"常见问题"的代码示例,其中的问题是帖子标题本身:
add_filter('posts_where','title_filter',10,2); $s1 = new WP_Query( array( 'post_type' => 'faq', 'posts_per_page' => -1, 'title_filter' => $q, 'title_filter_relation' => 'OR', 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'faq_answer', 'value' => $q, 'compare' => 'LIKE' ) ) )); remove_filter('posts_where','title_filter',10,2);
Building on other answers before me, to provide flexibility in the situation where you want to search a post that contains a word in a meta field OR in the title of the post, I give that option via the argument "title_filter_relation." In this implementation, I only allow for "OR" or "AND" inputs with a default of "AND."
function title_filter($where, &$wp_query){ global $wpdb; if($search_term = $wp_query->get( 'title_filter' )){ $search_term = $wpdb->esc_like($search_term); //instead of esc_sql() $search_term = ' \'%' . $search_term . '%\''; $title_filter_relation = (strtoupper($wp_query->get( 'title_filter_relation'))=='OR' ? 'OR' : 'AND'); $where .= ' '.$title_filter_relation.' ' . $wpdb->posts . '.post_title LIKE '.$search_term; } return $where; }
Here's an example of the code in action for a very simple post type "faq" where the question is the post title itself:
add_filter('posts_where','title_filter',10,2); $s1 = new WP_Query( array( 'post_type' => 'faq', 'posts_per_page' => -1, 'title_filter' => $q, 'title_filter_relation' => 'OR', 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'faq_answer', 'value' => $q, 'compare' => 'LIKE' ) ) )); remove_filter('posts_where','title_filter',10,2);
-
很有见识,将自定义"查询变量"添加到传递给" WP_Query"的查询args中,以便能够在"posts_where"过滤器中对其进行访问.Good insight, adding custom "query vars" to the query args passed to `WP_Query` in order to be able to access them within the `posts_where` filter.
- 1
- 2017-02-04
- Tom Auger
我需要对
WP_Query
上的LIKE
进行post_title
.我从这个常规的
post_title
开始:但是我实际上想在SQL中执行以下操作:
输出显示我期望的结果,但是我使用常规的
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
以显示结果.这不适用于
$wpdb->get_results()
.如何实现我在这里描述的内容?