WordPress.org

Ready to get started?Download WordPress

Forums

WP Super Cache
Prune_super_cache categories and scheduled posts (2 posts)

  1. RickyTF
    Member
    Posted 1 year ago #

    Hello, our site have +50.000 posts and we have no expiry time for WP Super Cache for the last 15 days. The performance has increased a lot with this and we resolved the issue with the categories not refresehing with this function developed by Biranit (found it in this entry):

    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 ) ) {
    		if (in_category(3, $post_id)) {
    			prune_super_cache( get_supercache_dir() . '/israel-and-the-region/', true );
    		}
    		if (in_category(4, $post_id)) {
    			prune_super_cache( get_supercache_dir() . '/jewish-times/', true );
    		}
    		if (in_category(5, $post_id)) {
    			prune_super_cache( get_supercache_dir() . '/israel-inside/', true );
    		}
    		if (in_category(6, $post_id)) {
    			prune_super_cache( get_supercache_dir() . '/ops-and-blogs/', true );
    		}
    		if (in_category(7, $post_id)) {
    			prune_super_cache( get_supercache_dir() . '/start-up-israel/', true );
    		}
    	}
    }
    add_action('save_post', 'rgbtoi_prune_category_cache', 15 );

    Now, as we have more categories, we've adapted that code in to this:

    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 ) ) {
    
    foreach((get_the_category()) as $category) {
    prune_super_cache( get_supercache_dir() . '/' .$category->category_nicename. '/' , true );
    
    }
    
    	}
    }
    add_action('save_post', 'rgbtoi_prune_category_cache', 15 );

    It works very well, but it causes two little issues:

    -Publish or update a post it's very slow. We thought it could be caused becouse of the function itself, thats why we've removed the al the "if" and implemented "$category->category_nicename" instead, but the issue when saving or publish continues.

    -The other thing is the scheduled posts, because when they are publishing, the categories aren't updating. We think it could be related to the 'publish_future_post' action, but honestly we don't know how to solve it in the same function.

    Any help will be very apreciated, and thanks a lot for this fantastic plugin.

    PS: Sorry for my poor english.

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

  2. Biranit
    Member
    Posted 11 months ago #

    Hi Ricky,

    Your function is correct, and it's what I use nowadays too.

    It should not slow down your publishing or updating at all. This is a very low-resources function. Try looking elsewhere.

    You could also lower the action priority, so it happens last:

    add_action('save_post', 'rgbtoi_prune_category_cache', 99 );

    As for scheduled posts, just add under your function another action:

    add_action('publish_future_post', 'rgbtoi_prune_category_cache', 99);

    Cheers,

    Bira

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic