From 7120a24a7a838f21288cade14a32569e5bcf9489 Mon Sep 17 00:00:00 2001 From: Anukasha Singh Date: Thu, 9 Apr 2026 10:51:33 +0530 Subject: [PATCH] changes to allow post_name in search --- src/wp-includes/class-wp-query.php | 12 ++- tests/phpunit/tests/query/searchColumns.php | 85 ++++++++++++++++++++- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index cf07b07d977c3..014910a214f91 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -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. @@ -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 ); @@ -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. @@ -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; } diff --git a/tests/phpunit/tests/query/searchColumns.php b/tests/phpunit/tests/query/searchColumns.php index 9ef30c2113920..f4fe1cfc8d242 100644 --- a/tests/phpunit/tests/query/searchColumns.php +++ b/tests/phpunit/tests/query/searchColumns.php @@ -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. * @@ -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', + ) + ); } /** @@ -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 ); @@ -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.' ); } @@ -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; } @@ -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; + } }