When calling ceo_get_adjacent_chapter() a second time in my chapter-oriented custom theme, current_chapter->menu_order is empty if W3 Total Cache's object caching is turned on.
This version of ceo_get_adjacent_chapter() fixes this by explicitly caching current_chapter->menu_order, and doesn't seem to have any side effects.
function ceo_get_adjacent_chapter($prev = false) {
global $post;
$current_chapter = get_the_terms($post->ID, 'chapters');
if (is_array($current_chapter)) { $current_chapter = reset($current_chapter); } else { return; }
// cache the calculation of the desired chapter to work around a weird intermittent bug with w3 total cache's object cache
$current_order = wp_cache_get( 'ceo_current_order_'.$current_chapter->slug );
if ( false === $current_order ) {
$current_order = $current_chapter->menu_order;
wp_cache_set( 'ceo_current_order_'.$current_chapter->slug, $current_order );
}
$find_order = (bool)$prev ? $current_order - 1 : $current_order + 1;
if (!$find_order) return false;
$args = array(
'orderby' => 'menu_order',
'order' => 'DESC',
'hide_empty' => 1,
'menu_order' => $find_order
);
$all_chapters = get_terms( 'chapters', $args );
if (!is_null($all_chapters)) {
foreach($all_chapters as $chapter) {
if ((int)$chapter->menu_order == $find_order) return $chapter;
}
}
return false;
}
When calling ceo_get_adjacent_chapter() a second time in my chapter-oriented custom theme, current_chapter->menu_order is empty if W3 Total Cache's object caching is turned on.
This version of ceo_get_adjacent_chapter() fixes this by explicitly caching current_chapter->menu_order, and doesn't seem to have any side effects.