WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 8 months ago

Last modified 8 months ago

#17037 closed defect (bug) (invalid)

get_the_title() will pass an empty title and invalid post ID to 'the_title' filter on invalid post ID

Reported by: kawauso Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Template Keywords: has-patch
Focuses: Cc:

Description

get_the_title() has a workaround for when an invalid post ID is passed, setting the title to an empty string and post ID to the requested ID, then passing this to 'the_title' filter.

Example:

function testme() {
	var_dump( get_the_title( 99999 ) );
}

function filterme( $title ) {
	return "[$title]";
}

add_action( 'wp_head', 'testme' );
add_filter( 'the_title', 'filterme' );

Is there a reason for this odd functionality?

Attachments (1)

17037.diff (1.5 KB) - added by solarissmoke 3 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 scribu3 years ago

  • Keywords needs-patch added

I think it should just return false instead.

solarissmoke3 years ago

comment:2 solarissmoke3 years ago

  • Keywords has-patch added; needs-patch removed

This was done in #11435:

Fix notices in get_the_title() when passed an invalid id. Also ensure that the filter gets the invalid id so a plugin can supply a default title instead.

I fail to see why you would want a default title for a post that doesn't exist...

comment:3 c3mdigital8 months ago

  • Keywords has-patch removed
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 3.1 deleted

Couldn't this be handled by doing a check if ( '0' == $post_id ) in your get_the_title filter? And I also fail to see why you would want a default title for a post that doesn't exist.

comment:4 SergeyBiryukov8 months ago

  • Keywords has-patch added
  • Milestone Awaiting Review deleted
  • Version set to 3.0
Note: See TracTickets for help on using tickets.