Support » Plugin: WP Super Cache » WP super-cache do not delete category pages when new post is posted

Viewing 9 replies - 1 through 9 (of 9 total)
  • floreal,

    I wrote a function in my theme’s functions.php to solve this issue.

    // prune cache of categories when post is saved
    function rgbtoi_prune_category_cache($post_id) {
    	if ( $post_id == null || empty($_POST) )
    		return;
    
    	if ( !isset( $_POST['post_type'] ) || $_POST['post_type']!='post' )
    		return; 
    
    	//verify post is not a revision
    	if ( !wp_is_post_revision( $post_id ) ) {
    
    		$categories = get_the_category($post_id);
    		if ($categories) {
    			foreach ($categories as $cat) {
    				prune_super_cache ( get_supercache_dir() . '/' . $cat->slug . '/', true );
    			}
    		}
    		$posttags = get_the_tags($post_id);
    		if ($posttags) {
      			foreach($posttags as $tag) {
         			prune_super_cache( get_supercache_dir() . '/topic/' . $tag->slug . '/', true );
      			}
    		}
    		$writers = get_the_terms($post_id, 'writers');
    		if ($writers) {
    			foreach($writers as $writer) {
    				prune_super_cache( get_supercache_dir() . '/writers/' . $writer->slug . '/', true);
    			}
    		}
    	}
    }
    add_action('save_post', 'rgbtoi_prune_category_cache', 15 );

    A few things for you to alter in the above code to make it suitable for your site:

    1) In my site, categories don’t have a permalink prefix like http://www.example.com/category/ – so modify the line

    prune_super_cache ( get_supercache_dir() . ‘/’ . $cat->slug . ‘/’, true );

    to

    prune_super_cache ( get_supercache_dir() . ‘/category/‘ . $cat->slug . ‘/’, true );

    if you do.

    2) In my site, post_tag prefix is “topic”, so once again – modify the line:

    prune_super_cache( get_supercache_dir() . ‘/topic/‘ . $tag->slug . ‘/’, true );

    to whatever suits you.

    3) I have a custom taxonomy called “writers” – if you have your own custom taxonomy, you can use the above piece and modify it to your needs. Alternatively, just remove that piece from the code.

    Hope it’s all clear. Let me know if you have questions.

    Cheers,

    Bira

    Thanks for your reply.

    Your fo=unction works but is not perfect for me :
    – If I publish an article from the wordpress administration, it works perfectly.
    – But I generally post my articles through an application and the article is send through the XML-RPC API of wordpress to my blog. Doing this, your function doesn’t work.
    – I still have a problem if I delete an article : the cache isn’t updated.

    Do you have any suggestion to solve my problem ?

    Thanks for your help, and sorry for my bad english,

    Floréal.

    Hi floreal,

    These are not a problem to handle.

    1) For the post publishing, add the following to the code you already got from above:

    add_action(‘xmlrpc_publish_post’, ‘rgbtoi_prune_category_cache’, 15 );

    2) For posts being deleted, try adding

    add_action(‘delete_post’, ‘rgbtoi_prune_category_cache’, 15);

    Let me know if it all works.

    Cheers,

    Bira

    Hi,

    Thank you for your suggestion. I have added this to my function.php.
    1) For post publishing throuh XML-RPC : it does’nt work (no cache updated)
    2) For posts being deleted : again, it doesn’t work (no cache updated)

    Other idea ?

    Thanks for the time you pass for help me.

    Floreal.

    floreal,

    Are the posts you publish are post_type = ‘post’, or are they custom posts?

    You can try the post transition hook, with a similar function – for both publishing and unpublishing (deleting) a post. It might help:

    function rgbtoi_post_transition( $new_status, $old_status, $post ) {
        if (
        		($old_status == 'publish' && $new_status != 'publish' ) // post unpublished
        		||
        		($old_status != 'publish' && new_status == 'publish') // post published
    		) {
    
    		$categories = get_the_category($post->ID);
    		if ($categories) {
    			foreach ($categories as $cat) {
    				prune_super_cache ( get_supercache_dir() . '/' . $cat->slug . '/', true );
    			}
    		}
    		$posttags = get_the_tags($post->ID);
    		if ($posttags) {
      			foreach($posttags as $tag) {
         			prune_super_cache( get_supercache_dir() . '/topic/' . $tag->slug . '/', true );
      			}
    		}
        }
    }
    add_action( 'transition_post_status', 'rgbtoi_post_transition', 10, 3 );

    Thanks !

    This solution works perfectly for me – for publishing through XML-RPC, and when a post is deleted !

    Are the posts you publish are post_type = 'post', or are they custom posts?

    I don’t know. But your first code works when I publish with the wordpress admin, so I suppose that post_type = ‘post’. I don’t know where I can see that.

    But my problem is now resolved, as I have said in the beginning of my message. Thnaks a lot for your help !

    Floréal.

    Glad I could help 🙂

    Plugin Author Donncha O Caoimh

    (@donncha)

    I added this in trunk, plus adding in a line of code to delete the post itself if it’s publish status changes. Thanks @biranit!

    Oh, brilliant. Thanks a lot Donncha 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘WP super-cache do not delete category pages when new post is posted’ is closed to new replies.