[GH-ISSUE #18] Conflict with W3 Total Cache in ceo_get_adjacent_chapter() #17

Closed
opened 2026-06-06 22:29:04 -04:00 by yindo · 1 comment
Owner

Originally created by @egypturnash on GitHub (Dec 23, 2015).
Original GitHub issue: https://github.com/Frumph/comic-easel/issues/18

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;
}
Originally created by @egypturnash on GitHub (Dec 23, 2015). Original GitHub issue: https://github.com/Frumph/comic-easel/issues/18 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. ``` php 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; } ```
yindo closed this issue 2026-06-06 22:29:04 -04:00
Author
Owner

@Frumph commented on GitHub (Dec 23, 2015):

add/replacing with current, https://github.com/Frumph/comic-easel/commit/c49a9ece1f813b7b33b4f7f5051440347e29a848

added egypturnash's github account as collaborator so they can make commits themself

<!-- gh-comment-id:166835089 --> @Frumph commented on GitHub (Dec 23, 2015): add/replacing with current, https://github.com/Frumph/comic-easel/commit/c49a9ece1f813b7b33b4f7f5051440347e29a848 added egypturnash's github account as collaborator so they can make commits themself
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Frumph/comic-easel#17