Skip to content

Instantly share code, notes, and snippets.

@salcode
Last active December 26, 2015 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salcode/7209047 to your computer and use it in GitHub Desktop.
Save salcode/7209047 to your computer and use it in GitHub Desktop.
WordPress code to increment Published times by one second for each post when multiple posts are Published in one call. This is a work-around for https://core.trac.wordpress.org/ticket/8107
<?php
add_action( 'transition_post_status', 'srf_publish_post_unique_date', 10, 3 );
function srf_publish_post_unique_date( $new_status, $old_status, $post ) {
// only perform operation when post is
// changing to a status of 'publish'
// otherwise (abort)
if ( 'publish' !== $new_status ) { return; }
// temporarily remove action from hook
// to prevent endless loop
remove_action( 'transition_post_status', 'srf_publish_post_unique_date' );
$post = srf_increment_post_time( $post );
// store updated post information to db
wp_update_post( $post );
// restore temporarily removed action from hook
add_action( 'transition_post_status', 'srf_publish_post_unique_date', 10, 3 );
}
function srf_increment_post_time($post) {
// track increment between calls
global $srf_post_time_increment;
if ( !isset( $srf_post_time_increment ) ) {
// first call requires no increment
// next call will increment by 1 sec
$srf_post_time_increment = 1;
return $post;
}
// retrieve time and increment
$gmt_time = strtotime( $post->post_date_gmt . ' GMT' );
// increment by 1 sec each time this function is called
$gmt_time += $srf_post_time_increment;
// update post object information
$post->post_date_gmt = date( 'Y-m-d H:i:s', $gmt_time );
$post->post_date = date( 'Y-m-d H:i:s', $gmt_time + ( get_option( 'gmt_offset') * HOUR_IN_SECONDS ) );
// increment the counter for next call
$srf_post_time_increment++;
return $post;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment