0

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 
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
    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.
    Jun 29, 2023 at 19:37

1 Answer 1

3

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.

11
  • Oooh lala! Thank you!
    – pg.
    Jun 29, 2023 at 18:58
  • You're welcome. 🙇‍♂️
    – Caleb
    Jun 29, 2023 at 19:00
  • I added the search column and didn't see it and if I look at the $posts_query->request it's unchanged. How exactly to I use the wp_query_search_exclusion_prefix? I tried giving it different values to no effect.
    – pg.
    Jun 29, 2023 at 19:23
  • Can you please update your question with an Update section to show your adjusted query?
    – Caleb
    Jun 29, 2023 at 19:24
  • Added, the query is not different at all, other than the different long random strings placed around the search terms.
    – pg.
    Jun 29, 2023 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.