WordPress.org

Make WordPress Core

#24299 closed enhancement (duplicate)

'save_post' action is a common bottleneck

Reported by: johnjamesjacoby Owned by:
Milestone: Priority: low
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

When reviewing code, I see almost daily that the 'save_post' action is used as a catch-all for saving post-meta from inside custom metaboxes. The problem with this approach is that 'save_post' runs even on internal post-types, like revisions, nav-menu items, auto-saves, etc...)

It's my experience that most developers have no idea that their code runs in these circumstances, and they often don't check the post_type of the post being saved, resulting in unnecessary calls to update_post_meta() and sometimes storing useless information in the database.

This issue becomes especially prevalent on sites with complex navigation menus, where sometimes hundreds of calls to 'save_post' can occur. The added overhead of bbPress and a few other CPT based plugins installed can ramp these wasted comparisons up into the thousands very quickly.

Yes... this can largely be considered an education issue. And $post is passed into 'save_post' so it's a simple comparison check to make sure the post_type matches your intentions. There's also transition_post_status that allows even more control.

Knowing this, I think there's still value in addressing this in core, by having a dedicated "save_post_$post_type" action directly after 'save_post'. It's straight-forward, easy to tweak existing code, and provides a simple way to avoid repeated loops through 'save_post' just to compare a post_type and bail a bunch of times.

Patch attached for proof of concept

Attachments (1)

24299.patch (519 bytes) - added by johnjamesjacoby 11 months ago.

Download all attachments as: .zip

Change History (5)

johnjamesjacoby11 months ago

comment:1 tw211311 months ago

Having had to interact with this myself and prevent multiple looping and "loop inception", I would love to see this available.

comment:2 johnbillion11 months ago

How does 24299.patch help with revisions and autosaves? They're post statuses, not post types.

Version 0, edited 11 months ago by johnbillion (next)

comment:3 DrewAPicture11 months ago

Seems sensible. +1

comment:4 SergeyBiryukov11 months ago

  • Keywords dev-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #16176.

Note: See TracTickets for help on using tickets.