Make WordPress Core

Changeset 54271


Ignore:
Timestamp:
09/21/2022 05:27:12 AM (22 months ago)
Author:
peterwilsoncc
Message:

Posts, Post Types: Fix WP_Query parameter used by get_page_by_title().

Fixes the call to WP_Query within get_page_by_title() by using the correct title parameter, title.

Modify the orderby parameter to prioritize the oldest published date over the smallest post ID. This ensures the behaviour matches that of the previous version of get_page_by_title().

The tests have been modified to include a populated post table to ensure the posts returned are matched by design rather than coincidence.

Follow up to [54234].

Props dd32, timothyblynjacobs, peterwilsoncc.
Fixes #56609.
See #36905.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r54267 r54271  
    57765776function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    57775777    $args  = array(
    5778         'post_title'             => $page_title,
     5778        'title'                  => $page_title,
    57795779        'post_type'              => $post_type,
    57805780        'post_status'            => get_post_stati(),
     
    57835783        'update_post_meta_cache' => false,
    57845784        'no_found_rows'          => true,
    5785         'orderby'                => 'ID',
     5785        'orderby'                => 'post_date ID',
    57865786        'order'                  => 'ASC',
    57875787    );
  • trunk/tests/phpunit/tests/post/getPageByTitle.php

    r54242 r54271  
    66 */
    77class Tests_Post_GetPageByTitle extends WP_UnitTestCase {
     8
     9    /**
     10     * Generate shared fixtures.
     11     *
     12     * These are not used in the tests but are rather used to populate the
     13     * posts table and ensure that the tests return the correct post object
     14     * by design rather than through chance.
     15     */
     16    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     17        // Fill the database with some pages.
     18        $factory->post->create_many(
     19            2,
     20            array(
     21                'post_type' => 'page',
     22            )
     23        );
     24
     25        // Fill the database with some attachments.
     26        $factory->post->create_many(
     27            2,
     28            array(
     29                'post_type' => 'attachment',
     30            )
     31        );
     32
     33        // Fill the database with some test post types.
     34        register_post_type( 'wptests_pt' );
     35        $factory->post->create_many(
     36            2,
     37            array(
     38                'post_type' => 'wptests_pt',
     39            )
     40        );
     41    }
     42
    843    /**
    944     * @ticket 36905
     
    4277
    4378        $this->assertSame( $page, $found->ID );
     79    }
     80
     81    /**
     82     * @ticket 36905
     83     * @ticket 56609
     84     */
     85    public function test_should_be_case_insensitive_match() {
     86        $page = self::factory()->post->create(
     87            array(
     88                'post_type'  => 'page',
     89                'post_title' => 'Foo',
     90            )
     91        );
     92
     93        $found = get_page_by_title( 'foo' );
     94
     95        $this->assertSame( $page, $found->ID );
     96    }
     97
     98    /**
     99     * Test the oldest published post is matched first.
     100     *
     101     * Per the docs: in case of more than one post having the same title,
     102     * it will check the oldest publication date, not the smallest ID.
     103     *
     104     * @ticket 36905
     105     * @ticket 56609
     106     */
     107    public function test_should_match_oldest_published_date_when_titles_match() {
     108        self::factory()->post->create(
     109            array(
     110                'post_type'  => 'page',
     111                'post_title' => 'foo',
     112            )
     113        );
     114
     115        $old_page = self::factory()->post->create(
     116            array(
     117                'post_type'  => 'page',
     118                'post_title' => 'foo',
     119                'post_date'  => '1984-01-11 05:00:00',
     120            )
     121        );
     122
     123        $found = get_page_by_title( 'foo' );
     124
     125        $this->assertSame( $old_page, $found->ID );
    44126    }
    45127
Note: See TracChangeset for help on using the changeset viewer.