Opened 15 years ago
Closed 8 years ago
#12976 closed enhancement (duplicate)
Add get_post_content()/get_post_excerpt() and save_postdata()/restore_postdata() for support.
Reported by: | mikeschinkel | Owned by: | |
---|---|---|---|
Milestone: | 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)
Change History (17)
#1
@
15 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:
↓ 3
@
15 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
@
15 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.
#8
@
12 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.
#11
@
9 years 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
@
9 years 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
@
9 years 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
#15
@
8 years ago
- Milestone Future Release deleted
- Resolution set to duplicate
- Status changed from reviewing to closed
Since get_the_excerpt()
now accepts a post ID/object parameter and we're struggling with the same problems in #36934, I'm closing this one as a duplicate. That ticket already has some tests and patches to reduce the usage of global variables and even a patch that uses the same approach as here.
Adds get_post_content(), get_post_excerpt() as well as save_postdata() and restore_postdata()