Make WordPress Core

Opened 23 months ago

Closed 22 months ago

Last modified 22 months ago

#56880 closed enhancement (duplicate)

Allow to select which columns to use when doing a search query

Reported by: petitphp's profile petitphp Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

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
// 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).

Change History (4)

This ticket was mentioned in PR #3510 on WordPress/wordpress-develop by @petitphp.


23 months ago
#1

  • Keywords has-patch has-unit-tests added

#2 @johnbillion
23 months ago

  • Version trunk deleted

Related / dupliadte: #43867

#3 @petitphp
22 months ago

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #43867.

#4 @desrosj
22 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.