WordPress.org

Ready to get started?Download WordPress

Forums

WP Super Cache
[resolved] WP super-cache do not delete category pages when new post is posted (10 posts)

  1. floreal
    Member
    Posted 1 year ago #

    Hi,

    I have a problem ! wp super-cache do not delete de category page into which I post a new article, when I post it.

    So, if a visitor go to this category page, he don't see the new article ! Is there a solution ?

    Version of the plugin I use : 1.3.2

    http://wordpress.org/extend/plugins/wp-super-cache/

  2. Biranit
    Member
    Posted 1 year ago #

    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

  3. floreal
    Member
    Posted 1 year ago #

    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.

  4. Biranit
    Member
    Posted 1 year ago #

    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

  5. floreal
    Member
    Posted 1 year ago #

    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.

  6. Biranit
    Member
    Posted 1 year ago #

    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 );
  7. floreal
    Member
    Posted 1 year ago #

    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.

  8. Biranit
    Member
    Posted 1 year ago #

    Glad I could help :)

  9. Donncha O Caoimh
    Member
    Plugin Author

    Posted 12 months ago #

    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!

  10. Biranit
    Member
    Posted 12 months ago #

    Oh, brilliant. Thanks a lot Donncha :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic