Make WordPress Core

Opened 10 months ago

Last modified 10 months ago

#59610 new defect (bug)

Post modified date cannot be updated through WP-CLI

Reported by: shreyasikhar26's profile shreyasikhar26 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.3.3
Component: Posts, Post Types Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description

As we all know, we can update post data using WP-CLI wp post update <post-id>, but if we try to update post-modified-date using the same CLI, the post gets updated with the current date-time rather than the passed date-time.

This CLI uses wp_update_post() in its implementation, so to fix this, we need to update its implementation or update the documentation of the CLI which will explain that post-modified-date cannot be updated through CLI.

Steps to reproduce this bug:

  1. Open the post on REST endpoint where post-modified-date will be visible. (for e.g. https://example.com/wp-json/wp/v2/posts/1 )
  2. Run the WP-CLI to update the post-modified-date: wp post update <post-id> --post_modified='2023-10-13T10:18:04' and reload the REST endpoint url.

Change History (5)

This ticket was mentioned in PR #5479 on WordPress/wordpress-develop by @shreyasikhar26.


10 months ago
#1

  • Keywords has-patch added

This ticket was mentioned in Slack in #cli by swissspidy. View the logs.


10 months ago

#3 @shreyasikhar26
10 months ago

  • Keywords 2nd-opinion added

I have linked the PR with the implementation, but I will need a second opinion on this.

Thanks

#4 @danielbachhuber
10 months ago

Thanks for the report, @shreyasikhar26 !

Did this work previously for you and recently break, or is this just a new finding generally?

#5 @brianhenryie
10 months ago

As I recall, it never worked that way.

There's a similar patch in #41227, closed in favour of #36595.

The behaviour of wp_update_post() (which is used by wp post update) is to always update the modified date to current_time() (inside wp_insert_post()).

The question then: should wp_post_update() always change post modified to the current time, or should it respect the post_modified values passed?

I expect this ticket will be closed in favor of the one mentioned.

But to discuss in the context of WP CLI...

What I have done in plugins in the past is:

<?php
/**
 * Don't change the last modified time.
 *
 * @see https://wordpress.stackexchange.com/questions/237878/how-to-prevent-wordpress-from-updating-the-modified-time
 *
 * @param array $new_post A WP_Post array.
 * @param array $old A WP_Post array.
 * @return array 
 */
$stop_modified_date_update = function ( $new_post, $old ) {
        $new_post['post_modified']     = $old['post_modified'];
        $new_post['post_modified_gmt'] = $old['post_modified_gmt'];
        return $new_post;
};
add_filter( 'wp_insert_post_data', $stop_modified_date_update, 10, 2 );

For WP CLI, it makes sense to either implement a function similar to that which does make the --post_modified= parameter take effect, or to warn users that it will do nothing.

Overall, I favour the core wp_update_post() function respecting the post_modified value, which should mean no changes needed in WP CLI.

Note: See TracTickets for help on using tickets.