WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#6284 closed enhancement (fixed)

add a the_post hook

Reported by: Denis-de-Bernardy Owned by:
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.5
Component: Optimization Keywords: has-patch tested commit
Focuses: Cc:

Description

Could we add a the_post hook, fired when the_post() gets called?

Its absence led me to this bug while investigating a workaround:

http://trac.wordpress.org/ticket/6283#preview

Thanks -- D

Attachments (1)

6284.diff (414 bytes) - added by Denis-de-Bernardy 6 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 @lloydbudd7 years ago

  • Milestone changed from 2.5 to 2.6

comment:2 @pishmishy7 years ago

It's not clear from your other ticket what you're trying to achieve and how it would be helped by this hook. Please could you supply us with further information.

@Denis-de-Bernardy6 years ago

comment:3 @Denis-de-Bernardy6 years ago

  • Keywords has-patch tested added
  • Milestone changed from 2.9 to 2.8

The purpose was (and still is) to apply filters conditionally on the post.

As a temporary workaround, I end up doing something like this:

on loop start, look at first post. if condition is met, remove all filters on the_content, except the one that checks the next post.

on the_content, look at next post, if condition is met, remove all filters on the_content, except the one that checks the next post, else restore the filters on the_content.

on loop end, restore all filters on the_content.

it's actually not all the filters, but much about. my purpose in doing so is to conditionally disable wpautop, texturize, etc. based on a postmeta's value.

comment:4 @azaozz6 years ago

Would a hook be better there or in setup_postdata()?

comment:5 @Denis-de-Bernardy6 years ago

Worth a discussion.

Personally, I use setup_postdata() on singular pages to set a few <meta> tags in my page's header using WP's built-in functions. I might be alone doing this, and would of course fix this accordingly. :-)

Leaving it up to you to decide, as long as the hook is around.

comment:6 @azaozz6 years ago

Couldn't that be done by using the_content as an action with higher priority? Something like:

add_filter( 'the_content', 'my_function', 1 );
function my_function( $content ) {
    // add or remove filters for this post...
    return $content;
}

comment:7 @Denis-de-Bernardy6 years ago

not really... foreach makes a copy of the the_content filters before running them, so this would only apply to the next post (see my comment above, it describes just that).

comment:8 @Denis-de-Bernardy6 years ago

here's another related ticket, that highlights the use for such a hook: #1595

comment:9 @Denis-de-Bernardy6 years ago

  • Keywords commit added

commit? wontfix?

comment:10 @jeremyclarke6 years ago

  • Cc jer@… added

Gigantic +1, I can't believe this isn't already available.

comment:11 @ryan6 years ago

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

(In [11213]) Add the_post action. Props Denis-de-Bernardy. fixes #6284

Note: See TracTickets for help on using tickets.