Make WordPress Core

Opened 4 years ago

Last modified 11 months ago

#36934 closed defect (bug)

Use of get_the_excerpt($post) is broken if post has no excerpt and you are inside a loop — at Version 4

Reported by: magicroundabout Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5
Component: Posts, Post Types Keywords: has-patch needs-testing has-unit-tests dev-feedback
Focuses: template Cc:

Description (last modified by jorbin)

In changeset #36319 (and #36321), a $post parameter was added to get_the_excerpt() in post-template.php. I was really excited when I discovered this but:

a) It's behaviour is inconsistent
b) It doesn't quite do what I'd hoped
c) It's broken in some circumstances

There are a few things wrong:

1) The behaviour is inconsistent depending on usage.

If you call get_the_excerpt() within the loop and if the post has no excerpt, then the post content will be stripped and truncated.

If you call it with the $post parameter and the post has no excerpt then nothing is returned.


2) If you are inside a loop - say you're using get_the_excerpt($post) in a shortcode in a post - then you will actually get back the truncated content of the post from the loop that you are in.

This is because wp_trim_excerpt( $text ) does get_the_content('') if $text is empty.

I think that the fix for this is to add an optional $post parameter to wp_trim_excerpt() and process it accordingly.

We'll also need to update default-filters.php to pass the extra parameter on the get_the_excerpt filter hook.

This change has the added benefit that get_the_excerpt($post) works consistently, fetching and trimming the post content if the post has no defined excerpt. Hooray!! (Been wanting this for YEARS!)

I'm working on a patch. It would be good to write a test for this too but I have no idea how. Happy to take feedback.

Change History (9)

4 years ago

Attempt at patch

#1 @magicroundabout
4 years ago

Steps to reproduce:

  • With a clean install of WordPress
  • Add the test-excerpt.php template to the default theme directory
  • Edit the "Sample page" to use the page template that I'm about to attach to this ticket
  • View the page

You should see the latest post's title, but the sample page's excerpt.

With my patch you get the latest post's excerpt as expected.

Last edited 4 years ago by magicroundabout (previous) (diff)

4 years ago

Sample theme template file for reproducing the issue

4 years ago

Corrected patch created from root rather than from wp-includes (sorry - newbie error)

#2 @achbed
4 years ago

Should we (rather than target just this function) also apply the $post parameter to the get_the_content function?

4 years ago

Updated patch to push $post parameter through get_the_content

#3 @achbed
4 years ago

  • Component changed from General to Formatting
  • Keywords has-patch added

#4 @jorbin
4 years ago

  • Description modified (diff)

4 years ago

Proposed unit test

Note: See TracTickets for help on using tickets.