6

It smelss like a bug but adding the post_modified in wp_insert_post isn't working:

$wp_test = array(
  'post_title'        => 'test',
  'post_content'      => 'test',
  'post_status'       => 'publish',
  'post_type'         => 'test',
  'post_author'       => 1,
  'post_date'         => '2015-01-22 22:00:12',
  'post_date_gmt'     => '2015-01-22 22:00:12',
  'post_modified'     => '2016-04-18 12:12:12',
  'post_modified_gmt' => '2016-04-18 12:12:12',
  'comment_status'    => 'closed'
);
$wp_id = wp_insert_post( $wp_test );

The post_modified date isnt working and gets the same value as post_date, not the wished '2016-04-18 12:12:12'. Is this a bug? i'm using wp 4.5 (twentysixteen theme). I fixed it by using the following code, but i think it could be more practical...

$wp_test = array(
  'post_title'        => 'test',
  'post_content'      => 'test',
  'post_status'       => 'publish',
  'post_type'         => 'test',
  'post_author'       => 1,
  'post_date'         => '2015-01-22 22:00:12',
  'post_date_gmt'     => '2015-01-22 22:00:12',
  'comment_status'    => 'closed'
);
$wp_id = wp_insert_post( $wp_test );

// ugly fix
$wpdb->query( "UPDATE $wpdb->posts SET post_modified = '2016-04-18 12:12:12',post_modified_gmt = '2016-04-18 12:12:12' WHERE ID = $wp_id");

1 Answer 1

7

It is not bug, actually WordPress does not allow (using arguments) to set post modification date. Internally WordPress set it to current time if you are updating an existing post else just set it to post date.

in /wp-includes/post.php#L3192 you can see wp_insert_post does not use this argument

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;
}

The documentation does not seems to me correct. I've created a ticket#36597 for this. Hopefully documentation will be corrected.

PS: However you can use wp_insert_post_data filter to set your custom modification date.

EDIT: WordPress already running a insert query and then running another SQL query just to update post modification time is not the good idea, better we can alter the date just before WordPress insert it to database. I think it will not cost you much than a separate SQL query.

First add a filter just before wp_insert_post and remove it so will not effect other insert functions.

add_filter( 'wp_insert_post_data', 'alter_post_modification_time', 99, 2 );
$wp_id = wp_insert_post( $wp_test );
remove_filter( 'wp_insert_post_data', 'alter_post_modification_time', 99, 2 );

In callback function set the modification time to which we have passed in wp_insert_post() function arguments. (Do not forgot to add post modification time which you've removed from arguments)

function alter_post_modification_time( $data , $postarr ) {
    if (!empty($postarr['post_modified']) && !empty($postarr['post_modified_gmt'])) {
        $data['post_modified'] = $postarr['post_modified'];
        $data['post_modified_gmt'] = $postarr['post_modified_gmt'];
    }

    return $data;
}
3
  • Yes I also created a ticket and now see the post_modified is an unused variable. Too bad it doesn't work, using an extra filter seems a bit bloated, i don't onderstand how to use the wp_insert_post_data, why would you dismiss the query solution i used?
    – Gijs
    Apr 19, 2016 at 20:14
  • @Gijs Yes I saw it later when my ticket marked as duplicated :D Anyway I've edited my answer because I can not explain it in comments :D
    – Sumit
    Apr 20, 2016 at 9:44
  • 1
    Holy cow - lesson learned. I spent half the day trying to solve the failure here. Thank you Sumit for posting this answer. Now I know that at least I'm not crazy and WP docs are not always perfect.
    – jdm2112
    May 3, 2017 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.