Skip to content

Commit 4c87073

Browse files
committed
Tweak WP_Query
1 parent 9b52804 commit 4c87073

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

src/wp-includes/canonical.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -986,22 +986,8 @@ function redirect_guess_404_permalink() {
986986
if ( $strict_guess ) {
987987
$query_args['name'] = get_query_var( 'name' );
988988
} else {
989-
// For loose matching (LIKE), we'll use a posts_where filter.
990-
$post_name_for_filter = get_query_var( 'name' );
991-
992-
// Store the filter callback so we can remove it later.
993-
$post_name_where_filter = static function ( $where, $query ) use ( $post_name_for_filter, $wpdb ) {
994-
// Only apply to our specific query.
995-
if ( isset( $query->query_vars['redirect_guess_404'] ) ) {
996-
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_name LIKE %s", $wpdb->esc_like( $post_name_for_filter ) . '%' );
997-
}
998-
return $where;
999-
};
1000-
1001-
add_filter( 'posts_where', $post_name_where_filter, 10, 2 );
1002-
1003-
// Mark this query so our filter knows to apply the LIKE clause.
1004-
$query_args['redirect_guess_404'] = true;
989+
$query_args['s'] = get_query_var( 's' );
990+
$query_args['search_columns'] = array( 'post_name' );
1005991
}
1006992

1007993
// 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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ 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'. Default empty array.
778778
* @type int $second Second of the minute. Default empty. Accepts numbers 0-59.
779779
* @type bool $sentence Whether to search by phrase. Default false.
780780
* @type bool $suppress_filters Whether to suppress filters. Default false.
@@ -1452,8 +1452,10 @@ protected function parse_search( &$query_vars ) {
14521452
$searchand = '';
14531453
$query_vars['search_orderby_title'] = array();
14541454

1455-
$default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );
1456-
$search_columns = ! empty( $query_vars['search_columns'] ) ? $query_vars['search_columns'] : $default_search_columns;
1455+
$default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );
1456+
$allowed_search_columns = $default_search_columns;
1457+
$allowed_search_columns[] = 'post_name';
1458+
$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 );
14591461
}
@@ -1473,7 +1475,7 @@ protected function parse_search( &$query_vars ) {
14731475
$search_columns = (array) apply_filters( 'post_search_columns', $search_columns, $query_vars['s'], $this );
14741476

14751477
// Use only supported search columns.
1476-
$search_columns = array_intersect( $search_columns, $default_search_columns );
1478+
$search_columns = array_intersect( $search_columns, $allowed_search_columns );
14771479
if ( empty( $search_columns ) ) {
14781480
$search_columns = $default_search_columns;
14791481
}

0 commit comments

Comments
 (0)