[wp-trac] [WordPress Trac] #36934: Use of get_the_excerpt($post) is broken if post has no excerpt and you are inside a loop

WordPress Trac noreply at wordpress.org
Fri Jun 3 21:00:15 UTC 2016


#36934: Use of get_the_excerpt($post) is broken if post has no excerpt and you are
inside a loop
-----------------------------+------------------------------
 Reporter:  magicroundabout  |       Owner:
     Type:  defect (bug)     |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Formatting       |     Version:  4.5.2
 Severity:  normal           |  Resolution:
 Keywords:  has-patch        |     Focuses:  template
-----------------------------+------------------------------
Description changed by jorbin:

Old description:

> 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.
>
> Well...sometimes...
>
> 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.

New description:

 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.

 Well...sometimes...

 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.

--

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36934#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list