Make WordPress Core

Opened 11 years ago

Closed 3 years ago

#24266 closed defect (bug) (wontfix)

update_post_meta doesn't change post modified date

Reported by: dennissmolek's profile DennisSmolek Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.1
Component: Options, Meta APIs Keywords: has-patch has-unit-tests 2nd-opinion close needs-refresh
Focuses: Cc:

Description

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 9 years ago.
propose update_post_modified_date
24266.2.diff (2.7 KB) - added by nofearinc 9 years ago.
updated logic
24266.3.diff (2.8 KB) - added by nofearinc 9 years ago.
24266.4.diff (6.4 KB) - added by swissspidy 8 years ago.

Download all attachments as: .zip

Change History (18)

#1 @nacin
11 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
10 years ago

  • Component changed from General to Options and Meta

#3 @wonderboymusic
10 years ago

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

@nofearinc
9 years ago

propose update_post_modified_date

#4 @nofearinc
9 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.

@nofearinc
9 years ago

updated logic

#5 @nofearinc
9 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.

@nofearinc
9 years ago

#6 @nofearinc
9 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
9 years ago

#30085 was marked as a duplicate.

#8 @jeremyfelt
9 years ago

#30492 was marked as a duplicate.

#9 @chriscct7
8 years ago

  • Keywords needs-unit-tests added

@swissspidy
8 years ago

#10 @swissspidy
8 years 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.

#11 @swissspidy
7 years ago

  • Keywords 2nd-opinion added

Related: #34775.

Now that I think of this again, it might just as well be plugin territory as it seems like this might cause some unwanted behaviour. Thus, it be great if someone else could jump in.

#12 @chriscct7
7 years ago

  • Keywords close added

This could cause a lot of unexpected things to occur. Many sites use the post modified field to show when the last public changes to post have occured, but a lot of plugins use meta for non-public internal things. Maybe limit the post modified change to only meta registered explicitly as public? Even still, I think this is better off as close as wontfix

#13 @hellofromTonya
3 years ago

  • Keywords needs-refresh added

This ticket was marked as a close candidate 4 years ago because it "could cause a lot of unexpected things to occur" and as @swissspidy notes "it might just as well be plugin territory".

What do we think today? Is this a wonfix and plugin territory? cc @swissspidy @johnbillion @nofearinc

Also marking as `needs-refresh as the patch fails against trunk.

#14 @johnbillion
3 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Agreed, this is plugin territory. Post meta is used for a lot of internal things, eg. the _edit_lock field which gets set whenever a post is opened for editing without any changes being made.

Note: See TracTickets for help on using tickets.