Make WordPress Core

Opened 22 months ago

Closed 22 months ago

Last modified 19 months ago

#56399 closed enhancement (wontfix)

get_page_by_title() Is Not Checking "publish" Post Status

Reported by: xmic's profile xmic Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Hello,

Issue
It is common expectation to use get_page_by_title() for us to get the published page that we wish to use. However, the current implementation does not take into consideration the post_status in the query.

Common Use Case Scenario
The current issue is that get_page_by_title() will get the draft post, and if this is used in conjunction with obtaining the URL, it will result in a 404 error for users.

Solution
Therefore, to take it a step further, we can introduce another parameter $post_status which accepts as a string or array of post_status defaulting to 'publish'.

This way, the function will not return a non-SEO friendly page, for example, if that page had been sent to the draft, because a user wishes to "re-create" the page without binning the previous one.

  • Michael

Attachments (1)

56399.diff (2.1 KB) - added by xmic 22 months ago.

Download all attachments as: .zip

Change History (11)

@xmic
22 months ago

#1 @xmic
22 months ago

  • Component changed from Bootstrap/Load to Posts, Post Types

This ticket was mentioned in PR #3104 on WordPress/wordpress-develop by michael-sumner.


22 months ago
#2

get_page_by_title() Is Not Checking "publish" Post Status

Trac ticket: https://core.trac.wordpress.org/ticket/56399

This ticket was mentioned in PR #3105 on WordPress/wordpress-develop by michael-sumner.


22 months ago
#3

get_page_by_title() Is Not Checking "publish" Post Status

Trac ticket: https://core.trac.wordpress.org/ticket/56399

#4 @xmic
22 months ago

Ignore PR #3104 use PR #3105

#6 follow-up: @joyously
22 months ago

The function already returns the Post object, so the status can be checked, if needed.
This seems a bit edge-case since this function would not be encountered by the user, and a plugin author could just as easily use get_post with more parameters, like this function does.
There are also other get_page_by* functions that behave the same as this one.

#7 @SergeyBiryukov
22 months ago

Hi there, welcome back to WordPress Trac! Thanks for the ticket.

Just noting that this was previously discussed in #20350 and decided against, specifically in comment:7:ticket:20350.

#8 in reply to: ↑ 6 @xmic
22 months ago

Replying to joyously:

This seems a bit edge-case since this function would not be encountered by the user, and a plugin author could just as easily use get_post with more parameters, like this function does.
There are also other get_page_by* functions that behave the same as this one.

A client's site wanted to get the page title e.g. "Employment" and ensuring that the title was such a page called that, return the permalink.

This is used by developers still, who may not be aware that farther down the line if a new post is to be used, then it cannot, because the older post had the word "Employment" but was marked as "non-publish". Only later down the line do they realise that the get_page_by_title function was returning non-publish statuses as well.

The function already returns the Post object, so the status can be checked, if needed.

It returns a post object, but not the correct one.

Replying to SergeyBiryukov:

Just noting that this was previously discussed in #20350 and decided against, specifically in comment:7:ticket:20350.

The fix provided allows an array of post_status. However I cannot understand why a "non-publish" post will want to be returned? Surely a knowledgeable developer will want to use the $post_status array to include a search for that "non-publish" status?

Last edited 22 months ago by xmic (previous) (diff)

#9 @johnbillion
22 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed
  • Version trunk deleted

There's nothing specific about the get_page_by_title() function which suggests that it only returns published posts, and there are many functions in WordPress that can return unpublished content. Unfortunately if a developer is using it this way then that's a mistake. As noted above, the status of the returned post should be checked if only a published post is required.

In addition, fetching a post by its title is not a particularly reliable solution for a client site. If they decide to change the title one day then their site will break. Using an explicit option so the appropriate page can be selected and then fetching the page by its ID is much more reliable.

Note: See TracTickets for help on using tickets.