Skip to content

Commit

Permalink
Posts, Post Types: Fix WP_Query parameter used by `get_page_by_titl…
Browse files Browse the repository at this point in the history
…e()`.

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.



git-svn-id: https://develop.svn.wordpress.org/trunk@54271 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Sep 21, 2022
1 parent 77e65ea commit 6c6a674
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5775,14 +5775,14 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
*/
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
$args = array(
'post_title' => $page_title,
'title' => $page_title,
'post_type' => $post_type,
'post_status' => get_post_stati(),
'posts_per_page' => 1,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'no_found_rows' => true,
'orderby' => 'ID',
'orderby' => 'post_date ID',
'order' => 'ASC',
);
$query = new WP_Query( $args );
Expand Down
82 changes: 82 additions & 0 deletions tests/phpunit/tests/post/getPageByTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@
* @covers ::get_page_by_title
*/
class Tests_Post_GetPageByTitle extends WP_UnitTestCase {

/**
* Generate shared fixtures.
*
* These are not used in the tests but are rather used to populate the
* posts table and ensure that the tests return the correct post object
* by design rather than through chance.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Fill the database with some pages.
$factory->post->create_many(
2,
array(
'post_type' => 'page',
)
);

// Fill the database with some attachments.
$factory->post->create_many(
2,
array(
'post_type' => 'attachment',
)
);

// Fill the database with some test post types.
register_post_type( 'wptests_pt' );
$factory->post->create_many(
2,
array(
'post_type' => 'wptests_pt',
)
);
}

/**
* @ticket 36905
*/
Expand Down Expand Up @@ -43,6 +78,53 @@ public function test_should_match_top_level_page() {
$this->assertSame( $page, $found->ID );
}

/**
* @ticket 36905
* @ticket 56609
*/
public function test_should_be_case_insensitive_match() {
$page = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Foo',
)
);

$found = get_page_by_title( 'foo' );

$this->assertSame( $page, $found->ID );
}

/**
* Test the oldest published post is matched first.
*
* Per the docs: in case of more than one post having the same title,
* it will check the oldest publication date, not the smallest ID.
*
* @ticket 36905
* @ticket 56609
*/
public function test_should_match_oldest_published_date_when_titles_match() {
self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'foo',
)
);

$old_page = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'foo',
'post_date' => '1984-01-11 05:00:00',
)
);

$found = get_page_by_title( 'foo' );

$this->assertSame( $old_page, $found->ID );
}

/**
* @ticket 36905
*/
Expand Down

0 comments on commit 6c6a674

Please sign in to comment.