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.


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.

| improve this answer | |
  • Thanks for a nice minute find :). BTW what about the second function (my_blog_transient_flusher())? Isn't that enough with edit_category and save_post hook to destroy the transient? – Mayeenul Islam Oct 14 '14 at 3:35
  • You can have that if you want to force delete it, but the whole point of a transient is the timeout. If you don't have a timeout, then you might as well just use set_option and get_option and such. Saves an extra database query. – Otto Oct 14 '14 at 4:03
  • Thanks @Otto, you showed me the way. And now I found there're many reasons here, why I'm on a wrong path. I need to study and practice this more. Thanks a lot. It's the accepted answer. :) – Mayeenul Islam Oct 14 '14 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.