admin管理员组文章数量:1122832
Hi I'm using WP query in WP 6.1.1 like this:
$posts_where = [
'post_type' => 'page',
'posts_per_page' => - 1,
];
If there are search and status inputs I use those:
$posts_where['s'] = $search;
$posts_where['post_status'] = $status;
And I add in meta queries based on input too if they exist, this isn't super relevant but it's complicated to write in MySQL so it's why I am not just writing it out:
$posts_where['meta_query'][] = [
'key' => 'content_variations',
'value' => '%',
'compare' => 'LIKE',
];
Then I query like this:
$posts_query = new \WP_Query($posts_where);
$posts = $posts_query->posts;
I want to change the search so that it will get posts where the value of $SEARCH can also match on post_name as well as title and content. I can see that the query that S makes is:
AND (((wp_posts.post_title LIKE '%[SEARCH]%') OR (wp_posts.post_content LIKE '%[SEARCH]%'))
is there a way in WP query (i.e. not just writing out the query) to add OR (wp_posts.post_name LIKE '%[SEARCH]%')
to that?
UPDATE
I added 'search_columns' to the array along with 's' like this:
$posts_where['s'] = $value;
$posts_where['search_columns'] = [
'post_content',
'post_name',
'post_title',
];
but I still get the same query:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '{[long string]}SEARCH{[long string]}') OR (wp_posts.post_excerpt LIKE '{[long string]}SEARCH{[long string]}') OR (wp_posts.post_content LIKE '{[long string]}SEARCH{[long string]}'))) AND ((wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private'))) ORDER BY wp_posts.post_title LIKE '{[long string]}SEARCH{[long string]}' DESC, wp_posts.post_date DESC
Hi I'm using WP query in WP 6.1.1 like this:
$posts_where = [
'post_type' => 'page',
'posts_per_page' => - 1,
];
If there are search and status inputs I use those:
$posts_where['s'] = $search;
$posts_where['post_status'] = $status;
And I add in meta queries based on input too if they exist, this isn't super relevant but it's complicated to write in MySQL so it's why I am not just writing it out:
$posts_where['meta_query'][] = [
'key' => 'content_variations',
'value' => '%',
'compare' => 'LIKE',
];
Then I query like this:
$posts_query = new \WP_Query($posts_where);
$posts = $posts_query->posts;
I want to change the search so that it will get posts where the value of $SEARCH can also match on post_name as well as title and content. I can see that the query that S makes is:
AND (((wp_posts.post_title LIKE '%[SEARCH]%') OR (wp_posts.post_content LIKE '%[SEARCH]%'))
is there a way in WP query (i.e. not just writing out the query) to add OR (wp_posts.post_name LIKE '%[SEARCH]%')
to that?
UPDATE
I added 'search_columns' to the array along with 's' like this:
$posts_where['s'] = $value;
$posts_where['search_columns'] = [
'post_content',
'post_name',
'post_title',
];
but I still get the same query:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '{[long string]}SEARCH{[long string]}') OR (wp_posts.post_excerpt LIKE '{[long string]}SEARCH{[long string]}') OR (wp_posts.post_content LIKE '{[long string]}SEARCH{[long string]}'))) AND ((wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private'))) ORDER BY wp_posts.post_title LIKE '{[long string]}SEARCH{[long string]}' DESC, wp_posts.post_date DESC
Share
Improve this question
edited Jun 29, 2023 at 20:44
pg.
asked Jun 29, 2023 at 18:39
pg.pg.
1611 silver badge6 bronze badges
2
|
1 Answer
Reset to default 6In WordPress 6.2+, within WP_Query
you can set search_columns
parameter (not documented at time of writing) to specify the fields to search:
$query = new WP_Query( array(
's' => 'search term',
'search_columns' => array( 'post_content', 'post_name', 'post_title' ),
) );
You can also use the post_search_columns
filter to adjust the search columns. You may need to use the wp_query_search_exclusion_prefix
filter to change the operator. See the definition for WP_Query::parse_search()
for more info.
For WordPress before 6.2, you're likely limited to using the posts_where
filter.
Update:
Thanks to comment from Amin, discovered that WordPress restricts the search_columns
values to post_title
, post_excerpt
, and post_content
columns. This means that the posts_where
filter is the only option.
本文标签: wpdbHow to use wpquery to search for posts where postcontent OR posttitle OR postname
版权声明:本文标题:wpdb - How to use wp-query to search for posts where post_content OR post_title OR post_name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303403a1931873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
meta_query
forces it to only search those posts that meet that post meta condition. Note that themeta_query
clause you added can get very expensive/slow. – Tom J Nowell ♦ Commented Jun 29, 2023 at 19:08