[wp-trac] [WordPress Trac] #54020: Add a filter to allow updating post without changing the modified dates

WordPress Trac noreply at wordpress.org
Thu Aug 26 22:40:11 UTC 2021


#54020: Add a filter to allow updating post without changing the modified dates
-------------------------+-------------------------------------------------
 Reporter:  pbearne      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:  has-unit-tests has-dev-note dev-
  Focuses:               |  feedback
-------------------------+-------------------------------------------------
 wp_update_post calls wp_insert_post which has code that always changes the
 modified date
 this causes problems when trying to fix posts via batch/wp-cli type
 operations


 {{{#!php
 <?php
 if ( $update || '0000-00-00 00:00:00' === $post_date ) {
    $post_modified     = current_time( 'mysql' );
    $post_modified_gmt = current_time( 'mysql', 1 );
 } else {
    $post_modified     = $post_date;
    $post_modified_gmt = $post_date_gmt;
 }
 }}}


 So you end with code like this to work around this

 {{{#!php
 <?php
 // get the date from the current post and reset it via the filter
 $function = function ( $data ) use ( $id ) {
         $post = get_post( $id );
         $data['post_modified']     = $post->post_date_gmt;
         $data['post_modified_gmt'] = $post->post_date_gmt;

         return $data;
 };

 add_filter( 'wp_insert_post_data', $function, 10, 2 );
 $updated = wp_update_post( $args );
 remove_filter( 'wp_insert_post_data', $function );
 }}}

 This function needs to make get_post() call for each insert and this slows
 the process in a big way

 I have added a filter that allows us to turn off the modification and set
 the date if needed

 {{{#!php
 <?php
 if ( $update || '0000-00-00 00:00:00' === $post_date ) {
                 /**
                  * Filters whether to preserve the modified dates when
 updating posts.
                  * Optionally allow you to set the date
                  *
                  * @since 2.9.0
                  *
                  * @param bool $trigger whether to preserve the modified
 date default: false
                  * @param array $postarr that will be saved.
                  *
                  * @return Bool|string|array true to keep the current
 date, String to set both date to same value
                  * keyed array to set both array( 'post_date' => date1,
 'post_date_gmt' => date2 )
                  */
                 $update_date = apply_filters(
 'wp_update_post_preserve_dates', false, $postarr );
                 if( false === $update_date ) {
                         $post_modified     = current_time( 'mysql' );
                         $post_modified_gmt = current_time( 'mysql', 1 );
                 } elseif ( ! is_array( $update_date ) && strtotime(
 $update_date ) ){
                         $post_modified     = $update_date;
                         $post_modified_gmt = get_gmt_from_date(
 $update_date );
                 } elseif ( is_array( $update_date ) && isset(
 $update_date['post_modified'] ) && isset(
 $update_date['post_modified_gmt'] ) ){
                         $post_modified      =
 $update_date['post_modified'];
                         $post_modified_gmt  =
 $update_date['post_modified_gmt'];
                 } else {
                         $post_modified     = $postarr['post_modified'];
                         $post_modified_gmt =
 $postarr['post_modified_gmt'];
                 }
         } else {
                 $post_modified     = $post_date;
                 $post_modified_gmt = $post_date_gmt;
         }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54020>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list