Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 12 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 14 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 15 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 27 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 29 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 30 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
Why is get_next_post() Not Working in WordPress
We’ve got a WordPress instance setup with lots of posts being created as Drafts. Then using the Bulk Actions, we change the status of the posts to Published. Everything seems great until it becomes evident the get_next_post()
and get_previous_post()
functions are not returning anything.
Why Isn’t this Working
It turns out this is a known WordPress bug, get_next_post, get_previous_post do not work for posts posted within same second and indeed the problem is when using the Bulk Actions to Publish, all the items are published at the same time (during the same second). The query for get_adjacent_post(), which does the heavy-lifting for get_next_post()
and get_previous_post()
does not work for multiple posts with the exact same Publish date.
How I Fixed It
I’ve added some code that increments the Published time by 1 second for each post when multiple posts are published in the same call.
<?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; | |
} |
What About Custom Post Types
modified 20131030 – The hooks used in this code apply to all post types ( including custom post types ).
photo credit: Photosightfaces via photopin cc
Leave a Reply