Skip to content

Commit eb2f5cd

Browse files
committed
Add search_position parameter to WP_Query for more refined search control
This commit introduces the `search_position` parameter to the `WP_Query` class, allowing developers to specify search behavior for posts: finding terms at the start, end, or anywhere within fields. Corresponding functionality in `canonical.php` has also been updated, and comprehensive unit tests were added to ensure reliability of the new behavior.
1 parent cf4de84 commit eb2f5cd

3 files changed

Lines changed: 358 additions & 10 deletions

File tree

src/wp-includes/canonical.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,9 @@ function redirect_guess_404_permalink() {
983983
if ( $strict_guess ) {
984984
$query_args['name'] = get_query_var( 'name' );
985985
} else {
986-
$query_args['s'] = get_query_var( 'name' );
987-
$query_args['search_columns'] = array( 'post_name' );
988-
$query_args['starts_with'] = true;
986+
$query_args['s'] = get_query_var( 'name' );
987+
$query_args['search_columns'] = array( 'post_name' );
988+
$query_args['search_position'] = 'start';
989989
}
990990

991991
// If any of post_type, year, monthnum, or day are set, use them to refine the query.

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,8 @@ public function fill_query_vars( $query_vars ) {
687687
* See WP_Date_Query::__construct().
688688
* @type int $day Day of the month. Default empty. Accepts numbers 1-31.
689689
* @type bool $exact Whether to search by exact keyword. Default false.
690-
* Cannot be used together with `$starts_with`.
691-
* @type bool $starts_with Whether to search starts with keyword. Default false.
690+
* Cannot be used together with `$search_position`.
691+
* @type bool $search_position Whether to search start, ends or is anywhere within keyword. Default anywhere.
692692
* Cannot be used together with `$exact`.
693693
* @type string $fields Post fields to query for. Accepts:
694694
* - '' Returns an array of complete post objects (`WP_Post[]`).
@@ -817,13 +817,13 @@ public function parse_query( $query = '' ) {
817817
$query_vars = &$this->query_vars;
818818
$this->query_vars_changed = true;
819819

820-
if ( ! empty( $query_vars['exact'] ) && ! empty( $query_vars['starts_with'] ) ) {
820+
if ( ! empty( $query_vars['exact'] ) && ! empty( $query_vars['search_position'] ) ) {
821821
_doing_it_wrong(
822822
__METHOD__,
823-
__( 'The `exact` and `starts_with` query parameters are mutually exclusive and cannot be used together.' ),
823+
__( 'The `exact` and `search_position` query parameters are mutually exclusive and cannot be used together.' ),
824824
'7.0.0'
825825
);
826-
$query_vars['starts_with'] = false;
826+
$query_vars['search_position'] = 'anywhere';
827827
}
828828

829829
if ( ! empty( $query_vars['robots'] ) ) {
@@ -1466,8 +1466,13 @@ protected function parse_search( &$query_vars ) {
14661466
if ( ! empty( $query_vars['exact'] ) ) {
14671467
$start = '';
14681468
$end = '';
1469-
} elseif ( ! empty( $query_vars['starts_with'] ) ) {
1470-
$start = '';
1469+
} elseif ( ! empty( $query_vars['search_position'] ) ) {
1470+
if ( 'start' === $query_vars['search_position'] ) {
1471+
$start = '';
1472+
}
1473+
if ( 'end' === $query_vars['search_position'] ) {
1474+
$end = '';
1475+
}
14711476
}
14721477

14731478
$searchand = '';
@@ -1983,6 +1988,10 @@ public function get_posts() {
19831988
}
19841989
}
19851990

1991+
if ( ! isset( $query_vars['search_position'] ) || ! in_array( $query_vars['search_position'], array( 'start', 'end', 'anywhere' ), true ) ) {
1992+
$query_vars['search_position'] = 'anywhere';
1993+
}
1994+
19861995
if ( ! isset( $query_vars['ignore_sticky_posts'] ) ) {
19871996
$query_vars['ignore_sticky_posts'] = false;
19881997
}

0 commit comments

Comments
 (0)