Make WordPress Core

Opened 6 years ago

Last modified 4 years ago

#42958 new enhancement

create callers for loop functions to accept post id

Reported by: tazotodua's profile tazotodua Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: 2nd-opinion
Focuses: Cc:


I've always been surprised, why the loop functions (like the_title, the_content, the_post_thumbnail and etc) doesnt have ability to accept post-id as argument.
I think that the get_..... functions is not replacement in MANY MANY cases, whereas the_... functions automatically generate all the needed attributes and tags. using the get_ function, it is hard to hardcode all those things manually.
I suggest to revise once again and why wont WP create callers, like i.e.

_the_content($post_id, [other parameters as of now...])
_the_title($post_id, [other parameters as of now...])

this will make the things much easier for developers. If you dont believe, see how hard it is to get the same result as the_content for specific post id (while outside of loop):

//save global variable
$target_post_id = 14;

//change global variable temporarily, to deceive WP
//ONLY NOW call get_the_content
$content = get_the_content( $more_link_text='Read more', $strip_teaser=false );
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );

//restore the global variable

echo $content;

this is real equivalent of the_content when you need to call outside of loop. Is it still arguable??

Change History (3)

This ticket was mentioned in Slack in #core by noisysocks. View the logs.

4 years ago

#2 @helen
4 years ago

  • Keywords 2nd-opinion added
  • Type changed from defect (bug) to enhancement

get_the_content() does at least have a $post parameter now, see #42814.

Specific to the proposal in the body of the ticket, I am not a big fan of adding faux-private functions like _the_content() and also think it's unwise to open up functions that are specific to The Loop to non-loop contexts without further examination of the underlying problem. So while I can imagine some use cases and problems this relates to, I'd like to start with understanding your particular scenarios where you need to get front-end formatted post content outside of a loop context, so any descriptions you're willing to provide will help move us toward possible solutions.

It seems to me that there is kind of a broader issue in WordPress about how to retrieve consistently formatted content for a specific context without directly applying filters yourself. There are excerpts and feeds and block editor and widgets and so on, all of which might want to retrieve the same post content but treat it in a different way. That's currently indicator by the different filter to be applied, but depending on what things we're solving for maybe it does make sense to add some kind of API for consistent retrieval.

I thought for sure this must be a duplicate request as it seems like the kind of thing people would probably be looking to do more regularly but I was unable to find one.

#3 @sabernhardt
4 years ago

The output with the $post parameter is not always the same as it would be for the post within the loop, yet the functions below worked for me outside the loop. (Most needed $post = 42 instead of just the post ID integer.)

<?php echo get_the_content( $post = 42 ); // output includes block editor comments ?>
<?php the_content( $post = 42 ); // output without HTML comments ?>

<?php echo get_the_title( 42 ); // this correctly gave the post title, though the_title($post=42) printed both post ID and title ?>

<?php echo get_the_post_thumbnail( 42 ); // image output may be adjusted to content width ?>
<?php the_post_thumbnail( $post = 42 ); // image output at full width ?>
Note: See TracTickets for help on using tickets.