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
  • aside from the post name, the inclusion of the meta_query forces it to only search those posts that meet that post meta condition. Note that the meta_query clause you added can get very expensive/slow. – Tom J Nowell Commented Jun 29, 2023 at 19:08
  • That's not a problem. I know meta_query will narrow it down and can be expensive, but I just want to search for post_title as well as the default search columns. – pg. Commented Jun 29, 2023 at 19:37
Add a comment  | 

1 Answer 1

Reset to default 6

In 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