The post_search_columns WordPress PHP filter allows you to modify the columns to search in a WP_Query search. By default, it searches in the post_title, post_excerpt, and post_content columns.
Usage
add_filter('post_search_columns', 'my_custom_search_columns', 10, 3); function my_custom_search_columns($search_columns, $search, $query) { // your custom code here return $search_columns; }
Parameters
$search_columns
(string[]): Array of column names to be searched.$search
(string): Text being searched.$query
(WP_Query): The current WP_Query instance.
More information
See WordPress Developer Resources: post_search_columns
Examples
Exclude post_excerpt from search
Exclude the post_excerpt column from the search results:
function exclude_post_excerpt_from_search($search_columns, $search, $query) { $key = array_search('post_excerpt', $search_columns); if ($key !== false) { unset($search_columns[$key]); } return $search_columns; } add_filter('post_search_columns', 'exclude_post_excerpt_from_search', 10, 3);
Include post_meta in search
Include a custom post_meta field in the search results:
function include_post_meta_in_search($search_columns, $search, $query) { $search_columns[] = 'post_meta_key'; return $search_columns; } add_filter('post_search_columns', 'include_post_meta_in_search', 10, 3);
Include only post_title in search
Limit the search to only the post_title column:
function search_only_post_title($search_columns, $search, $query) { return array('post_title'); } add_filter('post_search_columns', 'search_only_post_title', 10, 3);
Exclude post_content from search
Exclude the post_content column from the search results:
function exclude_post_content_from_search($search_columns, $search, $query) { $key = array_search('post_content', $search_columns); if ($key !== false) { unset($search_columns[$key]); } return $search_columns; } add_filter('post_search_columns', 'exclude_post_content_from_search', 10, 3);
Include custom table column in search
Include a custom table column in the search results:
function include_custom_table_column_in_search($search_columns, $search, $query) { $search_columns[] = 'custom_table_column'; return $search_columns; } add_filter('post_search_columns', 'include_custom_table_column_in_search', 10, 3);