Make WordPress Core


Ignore:
Timestamp:
02/07/2023 08:53:01 AM (4 years ago)
Author:
audrasjb
Message:

Query: Add a search_columns argument to control which fields are searched in a search query.

Previously, the s argument of the WP_Query::parse_query() method searched the post_title, post_excerpt, and post_content fields, with no way of controlling this apart from using the posts_search filter and adjusting the SQL manually. This changeset adds the ability to specify which fields are searched when performing a query, using the search_columns argument.

Props johnbillion, birgire, petitphp, audrasjb, costdev, mukesh27.
Fixes #43867.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r55104 r55248  
    205205                                'per_page',
    206206                                'search',
     207                                'search_columns',
    207208                                'slug',
    208209                                'status',
     
    15261527
    15271528                $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
     1529        }
     1530
     1531        /**
     1532         * Tests that Rest Post controller supports search columns.
     1533         *
     1534         * @ticket 43867
     1535         * @covers WP_REST_Posts_Controller::get_items
     1536         */
     1537        public function test_get_items_with_custom_search_columns() {
     1538                $id1 = self::factory()->post->create(
     1539                        array(
     1540                                'post_title'   => 'Title contain foo and bar',
     1541                                'post_content' => 'Content contain bar',
     1542                                'post_excerpt' => 'Excerpt contain baz',
     1543                        )
     1544                );
     1545                $id2 = self::factory()->post->create(
     1546                        array(
     1547                                'post_title'   => 'Title contain baz',
     1548                                'post_content' => 'Content contain foo and bar',
     1549                                'post_excerpt' => 'Excerpt contain foo, bar and baz',
     1550                        )
     1551                );
     1552
     1553                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1554                $request->set_param( 'search', 'foo bar' );
     1555                $request->set_param( 'search_columns', array( 'post_title' ) );
     1556                $response = rest_get_server()->dispatch( $request );
     1557                $this->assertSame( 200, $response->get_status(), 'Response should have a status code 200.' );
     1558                $data = $response->get_data();
     1559                $this->assertCount( 1, $data, 'Response should contain one result.' );
     1560                $this->assertSame( $id1, $data[0]['id'], 'Result should match expected value.' );
    15281561        }
    15291562
Note: See TracChangeset for help on using the changeset viewer.