Make WordPress Core

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

File 32824.post-slug-and-term-description-not-considered-for-search.patch, 3.4 KB (added by shewa, 7 months ago)

Patch to include post_name and term description in search queries.

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

     
    14521452                $searchand                          = '';
    14531453                $query_vars['search_orderby_title'] = array();
    14541454
    1455                 $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );
     1455                $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content', 'post_name' );
    14561456                $search_columns         = ! empty( $query_vars['search_columns'] ) ? $query_vars['search_columns'] : $default_search_columns;
    14571457                if ( ! is_array( $search_columns ) ) {
    14581458                        $search_columns = array( $search_columns );
  • src/wp-includes/class-wp-term-query.php

     
    11071107
    11081108                $like = '%' . $wpdb->esc_like( $search ) . '%';
    11091109
    1110                 return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
     1110                return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s)) OR (tt.description LIKE %s)', $like, $like, $like );
    11111111        }
    11121112
    11131113        /**
  • tests/phpunit/tests/query/search.php

     
    644644        public function filter_posts_search( $sql ) {
    645645                return $sql . ' /* posts_search */';
    646646        }
     647
     648        /**
     649         * Ticket #32824
     650         *
     651         * Test to check when search term mactched with the post_name
     652         * then it returns data
     653         *
     654         * @return void
     655         */
     656        public function test_search_matches_post_slug() {
     657                // Create a post where ONLY the slug contains the search keyword.
     658                $post_id = self::factory()->post->create(
     659                        array(
     660                                'post_title'   => 'Completely unrelated title',
     661                                'post_content' => 'Nothing to see here',
     662                                'post_name'    => 'my-special-search-slug',
     663                        )
     664                );
     665
     666                // Run a search query using a keyword from the slug.
     667                $query = new WP_Query(
     668                        array(
     669                                's'              => 'special-search',
     670                                'posts_per_page' => -1,
     671                        )
     672                );
     673
     674                // Assert that the post is returned.
     675                $this->assertContains(
     676                        $post_id,
     677                        wp_list_pluck( $query->posts, 'ID' ),
     678                        'Search query should return posts when the search term matches the post slug.'
     679                );
     680        }
     681
    647682}
  • tests/phpunit/tests/term/query.php

     
    12071207
    12081208                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
    12091209        }
     1210
     1211        /**
     1212         * Ticket #32824
     1213         *
     1214         * Test to check when search term mactched with the description column
     1215         * of wp_term_taxonomy table, it returns data
     1216         *
     1217         * @return void
     1218         */
     1219        public function test_search_matches_term_description() {
     1220
     1221                $term = wp_insert_term(
     1222                        'Some category',
     1223                        'category',
     1224                        array(
     1225                                'slug'        => 'some-category',
     1226                                'description' => 'Unique',
     1227                        )
     1228                );
     1229
     1230                $query = new WP_Term_Query(
     1231                        array(
     1232                                'taxonomy'   => 'test_tax',
     1233                                'search'     => 'unique',
     1234                                'hide_empty' => false,
     1235                        )
     1236                );
     1237
     1238                $terms = $query->get_terms();
     1239                $this->assertContains(
     1240                        $term['term_id'],
     1241                        wp_list_pluck( $terms, 'term_id' )
     1242                );
     1243        }
     1244
    12101245}