Skip to content

Commit 714d744

Browse files
committed
Tweak WP_Query to add starts_with parameter and refine 404 redirect logic
1 parent 4c87073 commit 714d744

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/wp-includes/canonical.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -919,13 +919,9 @@ function strip_fragment_from_url( $url ) {
919919
*
920920
* @since 2.3.0
921921
*
922-
* @global wpdb $wpdb WordPress database abstraction object.
923-
*
924922
* @return string|false The correct URL if one is found. False on failure.
925923
*/
926924
function redirect_guess_404_permalink() {
927-
global $wpdb;
928-
929925
/**
930926
* Filters whether to attempt to guess a redirect URL for a 404 request.
931927
*
@@ -980,14 +976,16 @@ function redirect_guess_404_permalink() {
980976
'update_post_meta_cache' => false,
981977
'update_post_term_cache' => false,
982978
'fields' => 'ids',
979+
'orderby' => 'none',
983980
);
984981

985982
// Handle strict vs. loose post_name matching.
986983
if ( $strict_guess ) {
987984
$query_args['name'] = get_query_var( 'name' );
988985
} else {
989-
$query_args['s'] = get_query_var( 's' );
986+
$query_args['s'] = get_query_var( 'name' );
990987
$query_args['search_columns'] = array( 'post_name' );
988+
$query_args['starts_with'] = true;
991989
}
992990

993991
// If any of post_type, year, monthnum, or day are set, use them to refine the query.
@@ -1025,11 +1023,6 @@ function redirect_guess_404_permalink() {
10251023

10261024
$query = new WP_Query( $query_args );
10271025

1028-
// Clean up the filter if we added it (remove only our specific callback).
1029-
if ( ! $strict_guess && isset( $post_name_where_filter ) ) {
1030-
remove_filter( 'posts_where', $post_name_where_filter, 10 );
1031-
}
1032-
10331026
if ( empty( $query->posts ) ) {
10341027
return false;
10351028
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ public function fill_query_vars( $query_vars ) {
660660
* @since 5.3.0 Introduced the `$meta_type_key` parameter.
661661
* @since 6.1.0 Introduced the `$update_menu_item_cache` parameter.
662662
* @since 6.2.0 Introduced the `$search_columns` parameter.
663+
* @since 7.0.0 Introduced the `$starts_with` parameter.
663664
*
664665
* @param string|array $query {
665666
* Optional. Array or string of Query parameters.
@@ -686,6 +687,7 @@ public function fill_query_vars( $query_vars ) {
686687
* See WP_Date_Query::__construct().
687688
* @type int $day Day of the month. Default empty. Accepts numbers 1-31.
688689
* @type bool $exact Whether to search by exact keyword. Default false.
690+
* @type bool $starts_with Whether to search start with keyword. Default false.
689691
* @type string $fields Post fields to query for. Accepts:
690692
* - '' Returns an array of complete post objects (`WP_Post[]`).
691693
* - 'ids' Returns an array of post IDs (`int[]`).
@@ -1448,7 +1450,15 @@ protected function parse_search( &$query_vars ) {
14481450
}
14491451
}
14501452

1451-
$n = ! empty( $query_vars['exact'] ) ? '' : '%';
1453+
$start = '%';
1454+
$end = '%';
1455+
if ( ! empty( $query_vars['exact'] ) ) {
1456+
$start = '';
1457+
$end = '';
1458+
} elseif ( ! empty( $query_vars['starts_with'] ) ) {
1459+
$start = '';
1460+
}
1461+
14521462
$searchand = '';
14531463
$query_vars['search_orderby_title'] = array();
14541464

@@ -1502,12 +1512,12 @@ protected function parse_search( &$query_vars ) {
15021512
$andor_op = 'OR';
15031513
}
15041514

1505-
if ( $n && ! $exclude ) {
1515+
if ( $end && ! $exclude ) {
15061516
$like = '%' . $wpdb->esc_like( $term ) . '%';
15071517
$query_vars['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
15081518
}
15091519

1510-
$like = $n . $wpdb->esc_like( $term ) . $n;
1520+
$like = $start . $wpdb->esc_like( $term ) . $end;
15111521

15121522
$search_columns_parts = array();
15131523
foreach ( $search_columns as $search_column ) {

0 commit comments

Comments
 (0)