Messing up with transient API – getting flushed on refresh

Following the _s theme I wanted to make my own version of Post Categories List with Transient, and here’s what I made:

function my_blog_categories() {
    global $post;
    if ( ( $get_post_categories = get_transient( 'this_blog_categories' ) === FALSE ) ) {
        $get_post_categories = get_the_category_list( ' | ', '', $post->ID );

        set_transient( 'this_blog_categories', $get_post_categories );
    return $get_post_categories;

function my_blog_transient_flusher() {
    delete_transient( 'this_blog_categories' );
add_action( 'edit_category', 'my_blog_transient_flusher' );
add_action( 'save_post',     'my_blog_transient_flusher' );

With WP Optimize if I clean the Transients, on first refresh, I can see the Categories below the post. But on the second-front-end-page-refresh they simply disappear.

Please not that, without the return $get_post_categories; it simply isn’t working. But what I know is get_the_category_list() itself is returning data.

So I’m in a clumsy mode – can’t figure out what am I doing wrong! Any assistance would be greatly appreciated.

Solutions Collecting From Web of "Messing up with transient API – getting flushed on refresh"

The first problem is that your parentheses are incorrect in your IF statement.

if ( ( $get_post_categories = get_transient( 'this_blog_categories' ) === FALSE ) )

Should be:

if ( ( $get_post_categories = get_transient( 'this_blog_categories' ) ) === false )

You’re wanting to assign the value, and then test that value against false. Because of your incorrect parentheses, what you’re doing is to compare the transient against false, then assign the result of that comparison to $get_post_categories. Comparisons have higher precedence than assignments in PHP.

That will likely fix your problem. However, you have another problem here:

set_transient( 'this_blog_categories', $get_post_categories );

Transients should have expiration times. Otherwise, your transient will never expire and you’ll never refresh the data at all.