Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 8 years ago

#19074 closed defect (bug) (invalid)

'transition_post_status' is executed before 'save_post'

Reported by: m66b's profile M66B Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

The action 'transition_post_status' is incorrectly executed before 'save_post' for custom post types

Change History (7)

#1 @nacin
13 years ago

I understand why it may make more sense to fire transition_post_status after save_post (in some situations), but that's not how wp_insert_post() is architected. We can't change it now.

#2 @M66B
13 years ago

  • Cc M66B added

But what is the reason this is done differently for custom post types?

The problem is that 'transition_post_status' is called before the post meta values can be saved, resulting in this problem.

#3 @nacin
13 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

It isn't done differently for custom post types. wp_insert_post() is used for pages, posts, custom post types.

You'll need to check your checkbox on transition_post_status in order to have the value ready for those hooks.

#4 @M66B
13 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

I have tested it again and it is really like this:

Regular post type: save_post, then transition_post_status and another save_post

Custom post types: transition_post_status, then save_post

Maybe wp_insert_post does it the right way, but the fact remains the order is different for custom post types.

You'll need to check your checkbox on transition_post_status in order to have the value ready for those hooks

The checkbox is checked in transition_post_status already, but the problem is that this works for the regular post type, but not for custom post types, because of the order the hooks are called.

#5 @nacin
13 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Check the post type of the post objects before doing anything.

If your post type supported revisions, you'd see both actions fire twice -- transition then save for the revision, transition then save for the post.

#6 @M66B
13 years ago

Thanks for your support!

#7 @nhangen
8 years ago

I hate to necro an old ticket, but I've been struggling for 3 days to discern why wp_transition_post_status wasn't working on new posts, and finally found this ticket, which points to the answer.

Because wp_transition_post_status fires early, we don't have access to a new post's post->ID, which means we can't grab metadata or a legitimate post object. We also don't actually know if the post was saved at all.

I understand that you can't modify this method, but the documentation should be updated in order to reflect the deficiencies of this hook. It's basically useless on anything but saved posts.

Note: See TracTickets for help on using tickets.