Skip to content

Commit 6c6a674

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@54271 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 77e65ea commit 6c6a674

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/wp-includes/post.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5775,14 +5775,14 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
57755775
*/
57765776
function 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(),
57815781
'posts_per_page' => 1,
57825782
'update_post_term_cache' => false,
57835783
'update_post_meta_cache' => false,
57845784
'no_found_rows' => true,
5785-
'orderby' => 'ID',
5785+
'orderby' => 'post_date ID',
57865786
'order' => 'ASC',
57875787
);
57885788
$query = new WP_Query( $args );

tests/phpunit/tests/post/getPageByTitle.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,41 @@
55
* @covers ::get_page_by_title
66
*/
77
class 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
1045
*/
@@ -43,6 +78,53 @@ public function test_should_match_top_level_page() {
4378
$this->assertSame( $page, $found->ID );
4479
}
4580

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 );
126+
}
127+
46128
/**
47129
* @ticket 36905
48130
*/

0 commit comments

Comments
 (0)