[wp-trac] [WordPress Trac] #56880: Allow to select which columns to use when doing a search query

WordPress Trac noreply at wordpress.org
Fri Oct 21 15:15:38 UTC 2022


#56880: Allow to select which columns to use when doing a search query
-------------------------+-----------------------------
 Reporter:  petitphp     |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Query        |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Currently, there is no way of specifying which columns will be searched by
 a `WP_Query` when using the search parameter (`s`). The query will look
 for the terms in `post_title`, `post_content` and `post_excerpt` columns.

 To restrict the search to specific columns, we can either manipulate the
 SQL clauses using the available hooks or write a custom SQL query. This is
 not practical, and it can lead to incorrect or unoptimized results.

 This ticket proposes to add a new parameter `search_columns` to the
 `WP_Query` where we can choose which columns will be searched :
 {{{#!php
 <?php
 // Searching on post title
 $q = new WP_Query(
     array(
         's' => 'the quick brown fox',
         'search_columns' => ['post_title'],
     )
 );

 // Searching on post title and excerpt
 $q = new WP_Query(
     array(
         's' => 'the quick brown fox',
         'search_columns' => ['post_title', 'post_excerpt'],
     )
 );
 }}}


 This new parameter takes an array of columns' names, with only the
 existing columns allowed as values (`post_title`, `post_content` and
 `post_excerpt`).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56880>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list