Make WordPress Core

Ticket #32824: 32824.post-slug-and-term-description-search.patch

File 32824.post-slug-and-term-description-search.patch, 3.7 KB (added by sachinrajcp123, 5 months ago)
Line 
1diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php
2index 3b6c9d8e1a..abcd123456 100644
3--- a/src/wp-includes/class-wp-query.php
4+++ b/src/wp-includes/class-wp-query.php
5@@ -1452,7 +1452,13 @@ class WP_Query {
6
7 $searchand = '';
8 $query_vars['search_orderby_title'] = array();
9- $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );
10+
11+ /*
12+ * Include post slug (post_name) in default search columns
13+ * to ensure consistency with term searches.
14+ *
15+ * @see https://core.trac.wordpress.org/ticket/32824
16+ */
17+ $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content', 'post_name' );
18
19 $search_columns = ! empty( $query_vars['search_columns'] ) ? $query_vars['search_columns'] : $default_search_columns;
20
21diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php
22index 9d4a2c1f33..efgh789012 100644
23--- a/src/wp-includes/class-wp-term-query.php
24+++ b/src/wp-includes/class-wp-term-query.php
25@@ -1107,7 +1107,14 @@ class WP_Term_Query {
26
27 $like = '%' . $wpdb->esc_like( $search ) . '%';
28
29- return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
30+ /*
31+ * Include term description in search results to align
32+ * with post search behavior.
33+ *
34+ * @see https://core.trac.wordpress.org/ticket/32824
35+ */
36+ return $wpdb->prepare(
37+ '((t.name LIKE %s) OR (t.slug LIKE %s) OR (tt.description LIKE %s))',
38+ $like,
39+ $like,
40+ $like
41+ );
42 }
43
44diff --git a/tests/phpunit/tests/query/search.php b/tests/phpunit/tests/query/search.php
45index 1a2b3c4d5e..9876543210 100644
46--- a/tests/phpunit/tests/query/search.php
47+++ b/tests/phpunit/tests/query/search.php
48@@ -647,6 +647,35 @@ class Tests_Query_Search extends WP_UnitTestCase {
49 }
50
51 /**
52+ * Ticket #32824
53+ *
54+ * Ensure search matches post slug (post_name)
55+ * when the keyword is not present in title or content.
56+ */
57+ public function test_search_matches_post_slug() {
58+ $post_id = self::factory()->post->create(
59+ array(
60+ 'post_title' => 'Unrelated title',
61+ 'post_content' => 'No matching keyword here',
62+ 'post_name' => 'my-special-search-slug',
63+ )
64+ );
65+
66+ $query = new WP_Query(
67+ array(
68+ 's' => 'special-search',
69+ 'posts_per_page' => -1,
70+ )
71+ );
72+
73+ $this->assertContains(
74+ $post_id,
75+ wp_list_pluck( $query->posts, 'ID' ),
76+ 'Post search should return results when matching the post slug.'
77+ );
78+ }
79+
80+ /**
81 * Tests search ordering by post title.
82 */
83 public function test_search_orderby_title() {
84diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php
85index aabbccdde1..1122334455 100644
86--- a/tests/phpunit/tests/term/query.php
87+++ b/tests/phpunit/tests/term/query.php
88@@ -1210,6 +1210,34 @@ class Tests_Term_Query extends WP_UnitTestCase {
89 }
90
91 /**
92+ * Ticket #32824
93+ *
94+ * Ensure term search matches taxonomy description.
95+ */
96+ public function test_search_matches_term_description() {
97+ $term = wp_insert_term(
98+ 'Some Category',
99+ 'category',
100+ array(
101+ 'slug' => 'some-category',
102+ 'description' => 'Unique description keyword',
103+ )
104+ );
105+
106+ $query = new WP_Term_Query(
107+ array(
108+ 'taxonomy' => 'category',
109+ 'search' => 'unique',
110+ 'hide_empty' => false,
111+ )
112+ );
113+
114+ $terms = $query->get_terms();
115+
116+ $this->assertContains(
117+ $term['term_id'],
118+ wp_list_pluck( $terms, 'term_id' )
119+ );
120+ }
121+
122 }