WordPress.org

Make WordPress Core

Opened 7 years ago

Last modified 14 months ago

#12976 reviewing enhancement

Add get_post_content()/get_post_excerpt() and save_postdata()/restore_postdata() for support.

Reported by: mikeschinkel Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: needs-unit-tests needs-patch
Focuses: Cc:

Description

Currently the get_the_content() and get_the_excerpt() functions return the values from the loop but do not allow a Post object to be passed. These functions in the patch (get_post_content() and get_post_excerpt()) save and then restore the global variables assigned by setup_postdata() so that get_the_content() can be called for a specific post.

The functions to save and restore the postdata are save_postdata() and restore_postdata() respectively, and they simply capture the values of the global variables set in setup_postdata() into an array and then restore them back from the array.

This is the first of a broader patch I hope to supply with functions for get_post_*() and the_post_*() that would each receive as their first parameter a Post object/post ID/post array and as a second an array of $args so that robust code can be written related to posts and so that there will be a set of functions for this with a consistent set of parameters.

Attachments (2)

save_restore_postdata_get_post_content_excerpt.diff (3.1 KB) - added by mikeschinkel 7 years ago.
Adds get_post_content(), get_post_excerpt() as well as save_postdata() and restore_postdata()
refresh-12976-2015-09-01.diff (12.6 KB) - added by MikeSchinkel 15 months ago.
Refreshed patch

Download all attachments as: .zip

Change History (16)

@mikeschinkel
7 years ago

Adds get_post_content(), get_post_excerpt() as well as save_postdata() and restore_postdata()

#1 @nacin
7 years ago

get_the_title() already takes a post ID, as does a few others. I'm pretty sure there is a ticket or two that suggest doing the same for get_the_content() and others.

We could probably do it so get_the_content() will not rely on the globals if you requested with a specific post in mind. Then again, it also does a lot of crazy things, such as paging, <!--more-->, and password protection.

#2 follow-up: @filosofo
7 years ago

Innumerable times I have to do something like the following just to get filtered content when the global variable stars aren't aligning just right:

$_post_obj = get_post($post_id);
echo apply_filters('the_content', $_post_obj->post_content);

And just try to do a custom-sized excerpt, using existing functions, in a way that makes any kind of sense.

What we need is something that you can pass an object ID to and return the excerpt or content, filtered. We don't need all those global dependencies, which are vestiges of the bad old days.

I've sat down to work on this problem before, but the backwards-compatibility issues have made me reach for the Tums instead.

#3 in reply to: ↑ 2 @mikeschinkel
7 years ago

  • Cc mikeschinkel@… added

Replying to nacin:

get_the_title() already takes a post ID, as does a few others. I'm pretty sure there is a ticket or two that suggest doing the same for get_the_content() and others.

We could probably do it so get_the_content() will not rely on the globals if you requested with a specific post in mind. Then again, it also does a lot of crazy things, such as paging, <!--more-->, and password protection.

I'm probably being a bit too idealistic here in hoping for consistency but one of the difficulties I have in using most of the get_*() and the_*() functions is how inconsistent the parameter sets are. It's reminds me of PHP itself. :)

What I'm hoping for (longer term) is a set of functions with consistent parameters and argument keys across the set of functions, i.e.:

$foo = get_post_*($post,$args);
the_post_*($post,$args);

Note I'm envisioning these as functions for programmers more than as "template tags" for end users.

Replying to filosofo:

What we need is something that you can pass an object ID to and return the excerpt or content, filtered. We don't need all those global dependencies, which are vestiges of the bad old days.

+1

My patch was but one way to attempt to skin that cat. I chose to write new functions as opposed to modifying existing functions but a better approach might be to refactor the existing ones into more reusable units. Not knowing the core dev's preference I didn't attempt a refactor. Either way, a workable solution would be grand.

#4 @kevinB
6 years ago

  • Cc kevinB added

#5 @demetris
6 years ago

  • Cc dkikizas@… added

#6 @nacin
6 years ago

  • Milestone changed from Awaiting Review to Future Release

#7 @travisnorthcutt
5 years ago

  • Cc travis@… added

#8 @mordauk
4 years ago

  • Cc pippin@… added

Let's please make this happen, though I'd suggest also allowing a post ID to be passed. I cannot even count the number of times I've had to setup a custom function to get a post excerpt based on the passed ID number.

#9 @chriscct7
15 months ago

  • Keywords has-patch needs-refresh added

@MikeSchinkel
15 months ago

Refreshed patch

#10 @MikeSchinkel
15 months ago

  • Keywords dev-feedback added; needs-refresh removed

Refreshed patch attached.

#11 @chriscct7
15 months ago

  • Milestone changed from Future Release to 4.4
  • Owner set to chriscct7
  • Status changed from new to reviewing

Looks good. Has some extra formatted that I'll revert, but otherwise looks good. Will do some more testing on this.

#12 @wonderboymusic
15 months ago

  • Keywords needs-unit-tests needs-patch added; has-patch dev-feedback removed

bizarre whitespace - this needs unit tests for use inline and while in the loop

#13 @wonderboymusic
15 months ago

  • Milestone changed from 4.4 to Future Release

this isn't pressing and there are no unit tests - we can look again when they materialize

#14 @chriscct7
14 months ago

  • Owner chriscct7 deleted
Note: See TracTickets for help on using tickets.