Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ public function fill_query_vars( $query_vars ) {
* character used for exclusion can be modified using the
* the 'wp_query_search_exclusion_prefix' filter.
* @type string[] $search_columns Array of column names to be searched. Accepts 'post_title',
* 'post_excerpt' and 'post_content'. Default empty array.
* 'post_excerpt', 'post_content', and 'post_name'.
* Default empty array.
* @type int $second Second of the minute. Default empty. Accepts numbers 0-59.
* @type bool $sentence Whether to search by phrase. Default false.
* @type bool $suppress_filters Whether to suppress filters. Default false.
Expand Down Expand Up @@ -1453,6 +1454,7 @@ protected function parse_search( &$query_vars ) {
$query_vars['search_orderby_title'] = array();

$default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );
$allowed_search_columns = array( 'post_title', 'post_excerpt', 'post_content', 'post_name' );
$search_columns = ! empty( $query_vars['search_columns'] ) ? $query_vars['search_columns'] : $default_search_columns;
if ( ! is_array( $search_columns ) ) {
$search_columns = array( $search_columns );
Expand All @@ -1461,10 +1463,12 @@ protected function parse_search( &$query_vars ) {
/**
* Filters the columns to search in a WP_Query search.
*
* The supported columns are `post_title`, `post_excerpt` and `post_content`.
* They are all included by default.
* The supported columns are `post_title`, `post_excerpt`, `post_content`,
* and `post_name`. By default, `post_title`, `post_excerpt`, and
* `post_content` are searched.
*
* @since 6.2.0
* @since 7.1.0 Added support for `post_name`.
*
* @param string[] $search_columns Array of column names to be searched.
* @param string $search Text being searched.
Expand All @@ -1473,7 +1477,7 @@ protected function parse_search( &$query_vars ) {
$search_columns = (array) apply_filters( 'post_search_columns', $search_columns, $query_vars['s'], $this );

// Use only supported search columns.
$search_columns = array_intersect( $search_columns, $default_search_columns );
$search_columns = array_intersect( $search_columns, $allowed_search_columns );
if ( empty( $search_columns ) ) {
$search_columns = $default_search_columns;
}
Expand Down
85 changes: 83 additions & 2 deletions tests/phpunit/tests/query/searchColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class Tests_Query_SearchColumns extends WP_UnitTestCase {
*/
protected static $pid3;

/**
* The post ID of the fixture post used for slug search tests.
*
* @since 7.1.0
* @var int $pid_slug
*/
protected static $pid_slug;

/**
* Create posts fixtures.
*
Expand Down Expand Up @@ -72,6 +80,16 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'post_content' => 'baz bar foo content',
)
);

self::$pid_slug = $factory->post->create(
array(
'post_status' => 'publish',
'post_title' => 'Taco',
'post_name' => 'burrito',
'post_content' => 'Enchilada',
'post_excerpt' => 'Torta',
)
);
}

/**
Expand Down Expand Up @@ -353,6 +371,7 @@ public function post_supported_search_column( $search_columns, $search, $wp_quer
* Tests that search columns ignores non-supported search columns from the `post_search_columns` filter.
*
* @ticket 43867
* @ticket 20044
*/
public function test_search_columns_should_not_be_filterable_with_non_supported_search_columns() {
add_filter( 'post_search_columns', array( $this, 'post_non_supported_search_column' ), 10, 3 );
Expand All @@ -363,7 +382,7 @@ public function test_search_columns_should_not_be_filterable_with_non_supported_
)
);

$this->assertStringNotContainsString( 'post_name', $q->request, "SQL request shouldn't contain post_name string." );
$this->assertStringNotContainsString( 'post_author', $q->request, "SQL request shouldn't contain post_author string." );
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts, 'Query results should be equal to the set.' );
}

Expand All @@ -376,7 +395,7 @@ public function test_search_columns_should_not_be_filterable_with_non_supported_
* @return string[] $search_columns Array of column names to be searched.
*/
public function post_non_supported_search_column( $search_columns, $search, $wp_query ) {
$search_columns = array( 'post_name' );
$search_columns = array( 'post_author' );
return $search_columns;
}

Expand Down Expand Up @@ -410,4 +429,66 @@ public function post_non_existing_search_column( $search_columns, $search, $wp_q
$search_columns = array( 'post_non_existing_column' );
return $search_columns;
}

/**
* Tests that `post_name` is not searched by default.
*
* @ticket 20044
*/
public function test_s_should_not_search_post_name_by_default() {
$q = new WP_Query(
array(
's' => 'burrito',
'fields' => 'ids',
)
);

$this->assertSame( array(), $q->posts );
}

/**
* Tests that search supports the `post_name` search column via the `post_search_columns` filter.
*
* @ticket 20044
*/
public function test_s_should_support_post_name_search_column_via_filter() {
add_filter( 'post_search_columns', array( $this, 'filter_add_post_name_search_column' ) );
$q = new WP_Query(
array(
's' => 'burrito',
'fields' => 'ids',
)
);
remove_filter( 'post_search_columns', array( $this, 'filter_add_post_name_search_column' ) );

$this->assertSame( array( self::$pid_slug ), $q->posts );
}

/**
* Tests that search supports the `post_name` search column via the `search_columns` query var.
*
* @ticket 20044
*/
public function test_s_should_support_post_name_search_column_via_query_var() {
$q = new WP_Query(
array(
's' => 'burrito',
'fields' => 'ids',
'search_columns' => array( 'post_name' ),
)
);

$this->assertSame( array( self::$pid_slug ), $q->posts );
}

/**
* Filter callback that adds `post_name` to the search columns.
*
* @param string[] $search_columns Array of column names to be searched.
* @return string[] $search_columns Array of column names to be searched.
*/
public function filter_add_post_name_search_column( $search_columns ) {
$search_columns[] = 'post_name';
return $search_columns;
}
}
Loading