WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#39949 closed defect (bug) (duplicate)

get_the_excerpt() in a widget doesn't work as intended

Reported by: madhatter2099 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.2
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

I'm trying to print out the excerpt for a page in a widget. The excerpt isn't explicitly set in the post, but is successfully added on other pages (ie on the category page I can use the_excerpt() and one will be created.

Inside a widget I'm trying to use

get_the_excerpt($post)

where $post is a WP_Post object. Instead of returning the excerpt for the given post the function returns the excerpt for the current post.

I then tried using the apply_filters() function as follows:

apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post))

and was met with the same problem. I also tried passing in the post ID instead of the object but get the same incorrect excerpt. I don't think this is what should be happening, I expect to get back an excerpt from the given post, not the current page's excerpt.

Change History (3)

#1 follow-up: @swissspidy
3 years ago

Hey there,

Welcome to Trac.

Sounds like you're affected by #36934. Basically, because of the way WordPress store some stuff in globals, the excerpt is built based on the wrong post object.

What happens when you explicitly add an excerpt for that post and display it?

#2 in reply to: ↑ 1 @madhatter2099
3 years ago

If I explicitly set an excerpt then it will display properly. It only seems to be when there's no excerpt that it doesn't get the correct excerpt. Using the apply_filters() function most likely isn't working since
get_post_field('post_excerpt', $post)
returns an empty string, which get_the_excerpt() won't know what to do with. I found the solution on another site so I'm not confident it's a proper solution anyway, I'd rather focus on getting the get_the_excerpt() function working.

I also see no reason that the WP_Post Class couldn't have an excerpt method added to it which either returns the excerpt or generates one.

Version 0, edited 3 years ago by madhatter2099 (next)

#3 @swissspidy
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Yeah a WP_Post::get_excerpt() method or something like that might be possible. But let's keep the discussion at #36934 :-)

Note: See TracTickets for help on using tickets.