Make WordPress Core

Opened 11 years ago

Closed 9 years ago

Last modified 9 years ago

#27246 closed enhancement (fixed)

Add function to get the excerpt outside the loop

Reported by: atoon's profile Atoon Owned by: swissspidy's profile swissspidy
Milestone: 4.5 Priority: normal
Severity: normal Version: 3.8.1
Component: Posts, Post Types Keywords: has-patch has-unit-tests commit
Focuses: template Cc:

Description

There's currently a function called get_the_excerpt, yet it don't work as the other similar ones as it don't support any arguments. I would like to have a core function that works like the_excerpt, but outside the loop by specifying an ID.

Attachments (3)

27246.diff (1.2 KB) - added by diddledani 9 years ago.
Repurpose deprecated argument to optionally indicate a post other than, the current from "The Loop", to excerpt
27246.2.diff (2.3 KB) - added by swissspidy 9 years ago.
27246.3.diff (2.5 KB) - added by swissspidy 9 years ago.

Download all attachments as: .zip

Change History (22)

#1 @SergeyBiryukov
11 years ago

  • Component changed from General to Posts, Post Types
  • Focuses template added

#2 @wonderboymusic
10 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

#3 follow-up: @husobj
10 years ago

I notice get_the_excerpt() current accepts 1 parameter which is $deprecated since 2.3

It looks like it used to be get_the_excerpt( $fakeit = true ) https://core.trac.wordpress.org/browser/tags/1.5/wp-includes/template-functions-post.php#L110 back as far as 1.5 but $fakeit it doesn't seem to be used anywhere so I presume we'd be OK to pass a post ID now like get_the_title()?

I'm happy to write a patch for this.

Last edited 10 years ago by husobj (previous) (diff)

#4 in reply to: ↑ 3 @DrewAPicture
10 years ago

Replying to husobj:

I notice get_the_excerpt() current accepts 1 parameter which is $deprecated since 2.3

It looks like it used to be get_the_excerpt( $fakeit = true ) https://core.trac.wordpress.org/browser/tags/1.5/wp-includes/template-functions-post.php#L110 back as far as 1.5 but $fakeit it doesn't seem to be used anywhere so I presume we'd be OK to pass a post ID now like get_the_title()?

We'd still need to pass the $deprecated argument, which definitely makes adding additional parameters a little clunky since you'd effectively call it as get_the_excerpt( '', $post_id );

#5 @husobj
10 years ago

I was trying to get to the bottom of what the old $fakeit was.

It looks like it used to expect a boolean true/false value as to wether to 'fake' the excerpt content but can't confirm that this was ever implemented - see #4439. The parameter is there but not implemented as far back as WP 1.5 (I haven't tried to research back further)

If it could be confirmed that it used to expect a boolean, could we then check for a numeric value for that first parameter and then only handle that as a Post ID if that's the case?

It would be nice to bring it inline with other functions like get_the_title()

#6 follow-up: @SergeyBiryukov
10 years ago

$fakeit was originally introduced in [87], unused since [2341].

I think it's safe to undeprecate it and treat it as a post ID if a numeric value or a post object is passed.

That still doesn't make it consistent with get_the_content() though...

#7 in reply to: ↑ 6 @husobj
10 years ago

Replying to SergeyBiryukov:

That still doesn't make it consistent with get_the_content() though...

True, hmm

#8 @chriscct7
9 years ago

  • Keywords needs-unit-tests added

@diddledani
9 years ago

Repurpose deprecated argument to optionally indicate a post other than, the current from "The Loop", to excerpt

#9 @diddledani
9 years ago

  • Keywords has-patch added; needs-patch removed

Patch uploaded, but we still need unit tests.

@swissspidy
9 years ago

#10 @swissspidy
9 years ago

27246.2.diff is an updated patch that also adds some unit tests for get_the_excerpt().

#11 @swissspidy
9 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed
  • Milestone changed from Future Release to 4.5

#12 @swissspidy
9 years ago

  • Keywords commit added

I think it's safe to undeprecate it and treat it as a post ID if a numeric value or a post object is passed.

27246.2.diff implements this and I think is safe to add. The original param was deprecated a long time ago and I often ran into this "bug" before.

@swissspidy
9 years ago

#13 @swissspidy
9 years ago

  • Owner set to swissspidy
  • Status changed from new to accepted

27246.3.diff adds a new test for the deprecated notice.

#14 @swissspidy
9 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 36319:

Posts: Add a $post parameter to get_the_excerpt().

This allows getting the excerpt for a specific post, similar to how most other template tags work.
A deprecation notice is thrown if a boolean value is passed, which is deprecated since 2.3 and has not been used for a long time.
Adds unit tests.

Fixes #27246.

#15 @swissspidy
9 years ago

In 36320:

Add tests missed and announced in [36319].

See #27246.

#16 @markoheijnen
9 years ago

With similar ones I compare it with the_content and that one also not have it. I rather would revert it since they are more like theme functions which are generally used in a loop.

Isn't it an option to put functions like this in WP_Post? and change the theme functions to call the methods in WP_Post. As a plugin developer I would love to use WP_Post more and doing something like this would be a good step.

#17 @swissspidy
9 years ago

With similar template tags I meant get_permalink(), get_the_title() and basically any get_ function. They all allow passing a $post while their the_ equivalents display the values for the current post. In theme templates, the_excerpt() is usually used, whereas get_the_excerpt( $post ) can also be used by plugins — without manually needing to apply filters.

Isn't it an option to put functions like this in WP_Post? and change the theme functions to call the methods in WP_Post. As a plugin developer I would love to use WP_Post more and doing something like this would be a good step.

Oh, I think it is. I haven't found an existing ticket for that, but I've seen plenty of code examples of custom Post decorator classes implementing something similar.

#18 follow-up: @markoheijnen
9 years ago

That's a good point. I also made myself guilty with copy/pasting the code of get_the_excerpt()/the_excerpt. So it is a good thing to have. I'm just thinking how the future would look like. Would be great to have a roadmap for WP_Post and related classes.

#19 in reply to: ↑ 18 @swissspidy
9 years ago

Replying to markoheijnen:

That's a good point. I also made myself guilty with copy/pasting the code of get_the_excerpt()/the_excerpt. So it is a good thing to have. I'm just thinking how the future would look like. Would be great to have a roadmap for WP_Post and related classes.

For the record, I just found #12267 about this. It was recently closed as maybelater, but if there's interest in it.. Why not?

Note: See TracTickets for help on using tickets.