Skip to content

Commit 7120a24

Browse files
committed
changes to allow post_name in search
1 parent 570cc92 commit 7120a24

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

src/wp-includes/class-wp-query.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ public function fill_query_vars( $query_vars ) {
774774
* character used for exclusion can be modified using the
775775
* the 'wp_query_search_exclusion_prefix' filter.
776776
* @type string[] $search_columns Array of column names to be searched. Accepts 'post_title',
777-
* 'post_excerpt' and 'post_content'. Default empty array.
777+
* 'post_excerpt', 'post_content', and 'post_name'.
778+
* Default empty array.
778779
* @type int $second Second of the minute. Default empty. Accepts numbers 0-59.
779780
* @type bool $sentence Whether to search by phrase. Default false.
780781
* @type bool $suppress_filters Whether to suppress filters. Default false.
@@ -1453,6 +1454,7 @@ protected function parse_search( &$query_vars ) {
14531454
$query_vars['search_orderby_title'] = array();
14541455

14551456
$default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );
1457+
$allowed_search_columns = array( 'post_title', 'post_excerpt', 'post_content', 'post_name' );
14561458
$search_columns = ! empty( $query_vars['search_columns'] ) ? $query_vars['search_columns'] : $default_search_columns;
14571459
if ( ! is_array( $search_columns ) ) {
14581460
$search_columns = array( $search_columns );
@@ -1461,10 +1463,12 @@ protected function parse_search( &$query_vars ) {
14611463
/**
14621464
* Filters the columns to search in a WP_Query search.
14631465
*
1464-
* The supported columns are `post_title`, `post_excerpt` and `post_content`.
1465-
* They are all included by default.
1466+
* The supported columns are `post_title`, `post_excerpt`, `post_content`,
1467+
* and `post_name`. By default, `post_title`, `post_excerpt`, and
1468+
* `post_content` are searched.
14661469
*
14671470
* @since 6.2.0
1471+
* @since 7.1.0 Added support for `post_name`.
14681472
*
14691473
* @param string[] $search_columns Array of column names to be searched.
14701474
* @param string $search Text being searched.
@@ -1473,7 +1477,7 @@ protected function parse_search( &$query_vars ) {
14731477
$search_columns = (array) apply_filters( 'post_search_columns', $search_columns, $query_vars['s'], $this );
14741478

14751479
// Use only supported search columns.
1476-
$search_columns = array_intersect( $search_columns, $default_search_columns );
1480+
$search_columns = array_intersect( $search_columns, $allowed_search_columns );
14771481
if ( empty( $search_columns ) ) {
14781482
$search_columns = $default_search_columns;
14791483
}

tests/phpunit/tests/query/searchColumns.php

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class Tests_Query_SearchColumns extends WP_UnitTestCase {
4141
*/
4242
protected static $pid3;
4343

44+
/**
45+
* The post ID of the fixture post used for slug search tests.
46+
*
47+
* @since 7.1.0
48+
* @var int $pid_slug
49+
*/
50+
protected static $pid_slug;
51+
4452
/**
4553
* Create posts fixtures.
4654
*
@@ -72,6 +80,16 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
7280
'post_content' => 'baz bar foo content',
7381
)
7482
);
83+
84+
self::$pid_slug = $factory->post->create(
85+
array(
86+
'post_status' => 'publish',
87+
'post_title' => 'Taco',
88+
'post_name' => 'burrito',
89+
'post_content' => 'Enchilada',
90+
'post_excerpt' => 'Torta',
91+
)
92+
);
7593
}
7694

7795
/**
@@ -353,6 +371,7 @@ public function post_supported_search_column( $search_columns, $search, $wp_quer
353371
* Tests that search columns ignores non-supported search columns from the `post_search_columns` filter.
354372
*
355373
* @ticket 43867
374+
* @ticket 20044
356375
*/
357376
public function test_search_columns_should_not_be_filterable_with_non_supported_search_columns() {
358377
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_
363382
)
364383
);
365384

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

@@ -376,7 +395,7 @@ public function test_search_columns_should_not_be_filterable_with_non_supported_
376395
* @return string[] $search_columns Array of column names to be searched.
377396
*/
378397
public function post_non_supported_search_column( $search_columns, $search, $wp_query ) {
379-
$search_columns = array( 'post_name' );
398+
$search_columns = array( 'post_author' );
380399
return $search_columns;
381400
}
382401

@@ -410,4 +429,66 @@ public function post_non_existing_search_column( $search_columns, $search, $wp_q
410429
$search_columns = array( 'post_non_existing_column' );
411430
return $search_columns;
412431
}
432+
433+
/**
434+
* Tests that `post_name` is not searched by default.
435+
*
436+
* @ticket 20044
437+
*/
438+
public function test_s_should_not_search_post_name_by_default() {
439+
$q = new WP_Query(
440+
array(
441+
's' => 'burrito',
442+
'fields' => 'ids',
443+
)
444+
);
445+
446+
$this->assertSame( array(), $q->posts );
447+
}
448+
449+
/**
450+
* Tests that search supports the `post_name` search column via the `post_search_columns` filter.
451+
*
452+
* @ticket 20044
453+
*/
454+
public function test_s_should_support_post_name_search_column_via_filter() {
455+
add_filter( 'post_search_columns', array( $this, 'filter_add_post_name_search_column' ) );
456+
$q = new WP_Query(
457+
array(
458+
's' => 'burrito',
459+
'fields' => 'ids',
460+
)
461+
);
462+
remove_filter( 'post_search_columns', array( $this, 'filter_add_post_name_search_column' ) );
463+
464+
$this->assertSame( array( self::$pid_slug ), $q->posts );
465+
}
466+
467+
/**
468+
* Tests that search supports the `post_name` search column via the `search_columns` query var.
469+
*
470+
* @ticket 20044
471+
*/
472+
public function test_s_should_support_post_name_search_column_via_query_var() {
473+
$q = new WP_Query(
474+
array(
475+
's' => 'burrito',
476+
'fields' => 'ids',
477+
'search_columns' => array( 'post_name' ),
478+
)
479+
);
480+
481+
$this->assertSame( array( self::$pid_slug ), $q->posts );
482+
}
483+
484+
/**
485+
* Filter callback that adds `post_name` to the search columns.
486+
*
487+
* @param string[] $search_columns Array of column names to be searched.
488+
* @return string[] $search_columns Array of column names to be searched.
489+
*/
490+
public function filter_add_post_name_search_column( $search_columns ) {
491+
$search_columns[] = 'post_name';
492+
return $search_columns;
493+
}
413494
}

0 commit comments

Comments
 (0)