Make WordPress Core

Opened 3 years ago

Last modified 11 months ago

#24266 new defect (bug)

update_post_meta doesn't change post modified date

Reported by: DennisSmolek Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.5.1
Component: Options, Meta APIs Keywords: has-patch has-unit-tests
Focuses: Cc:


In building an application I wanted to query for posts that were modified since a specific date/time.

The only issue is the majority of our changes are to post meta and not to post content, this means these changes don't show up in the query.

To solve this I'm tying into the action, but I feel like this is something that should be done by default.

Attachments (4)

24266.diff (865 bytes) - added by nofearinc 2 years ago.
propose update_post_modified_date
24266.2.diff (2.7 KB) - added by nofearinc 2 years ago.
updated logic
24266.3.diff (2.8 KB) - added by nofearinc 2 years ago.
24266.4.diff (6.4 KB) - added by swissspidy 11 months ago.

Download all attachments as: .zip

Change History (14)

#1 @nacin
3 years ago

Changing this would change behavior in a potentially un-expected way.

If wp_insert_post() added the ability to change metadata (something we've talked about), this could be solved. Interesting. I'm not even sure the easiest way to "bump" a post's modified date.

#2 @nacin
3 years ago

  • Component changed from General to Options and Meta

#3 @wonderboymusic
2 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

2 years ago

propose update_post_modified_date

#4 @nofearinc
2 years ago

  • Keywords has-patch added; needs-patch removed

A possible solution is a helper function update_post_modified_date that would update the modified date on add, update and delete. Sample use:

add_action( 'updated_postmeta', 'test_update_post_date', 10, 4 );
add_action( 'deleted_post_meta', 'test_update_post_date', 10, 4 );
add_action( 'added_post_meta', 'test_update_post_date', 10, 4 );

function test_update_post_date( $meta_id, $post_id, $meta_key, $meta_value ) {
	update_post_modified_date( $post_id );

I'm not a great fan of the the lack of a common action for updating (also, deleted_post_meta includes $meta_ids instead of $meta_id), but it could work.

2 years ago

updated logic

#5 @nofearinc
2 years ago

Moved to a core function that uses a global collection picking all changed post IDs where postmeta was changed, and updating the DB with cache clearing during the shutdown hook.

2 years ago

#6 @nofearinc
2 years ago

So, the first patch implements the idea of a helper function that could be applied by a plugin as discussed with nacin. The second one uses the global array for performance reasons but runs in core and collects all post IDs with updated metadata which is what we discussed with wonderboymusic. The third update updates only post metadata which was found thanks to an issue with Query Monitor by johnbillion that we debugged here.

#7 @johnbillion
2 years ago

#30085 was marked as a duplicate.

#8 @jeremyfelt
2 years ago

#30492 was marked as a duplicate.

#9 @chriscct7
11 months ago

  • Keywords needs-unit-tests added

11 months ago

#10 @swissspidy
11 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

I feel like method 3 using the shutdown hook is not ideal, because it runs after all output is sent. For example, when updating post meta using the REST API, you want to return the new modified date, which this wouldn't allow for. Also, using global variables isn't ideal and shutdown is hard to test.

On the other hand, there can be lots of meta data changes in a single request, so using the shutdown hook is definitely more performant.

Long story short, 24266.4.diff is an updated patch that adds unit tests and also uses only 1 single DB query in update_post_modified_dates().

Pinging @rmccue for feedback.

Note: See TracTickets for help on using tickets.