#56880 closed enhancement (duplicate)
Allow to select which columns to use when doing a search query
Reported by: | 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
Trac ticket: https://core.trac.wordpress.org/ticket/56880