WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 20 months ago

#12976 new 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:
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 (1)

save_restore_postdata_get_post_content_excerpt.diff (3.1 KB) - added by mikeschinkel 4 years ago.
Adds get_post_content(), get_post_excerpt() as well as save_postdata() and restore_postdata()

Download all attachments as: .zip

Change History (9)

mikeschinkel4 years ago

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

comment:1 nacin4 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.

comment:2 follow-up: filosofo4 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.

comment:3 in reply to: ↑ 2 mikeschinkel4 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.

comment:4 kevinB4 years ago

  • Cc kevinB added

comment:5 demetris4 years ago

  • Cc dkikizas@… added

comment:6 nacin3 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:7 travisnorthcutt2 years ago

  • Cc travis@… added

comment:8 mordauk20 months 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.

Note: See TracTickets for help on using tickets.