简体   繁体   中英

How to detect a change in the number of posts in WordPress?

This is more a request for a code-review than a bug. The question is if this is the "right way" of writing the following function.

What I would like is to run a php cron task that would check if the number of posts in the blog has changed, and if so - to erase the homepage cached html.

I can use the WP Crontrol plugin to set up a php cron task. What I need is the functions to create a global variable that would be stored somewhere of the number of posts (I used the wp_options table), and each time to ask the server what is the current number of posts. And that if there is a difference, to clear the cache. Below is the function I ended up writing. Does it make sense, or is there something I should have done differently (/better?)

if_new_posts_delete_homepage_cache = function() {
    // get current number of posts
    // https://codex.wordpress.org/Function_Reference/wp_count_posts
    $count_posts = wp_count_posts();
    $new_number_of_posts = $count_posts->publish;

    // https://developer.wordpress.org/reference/functions/get_option/
    // set number of posts for the first time
    // some code that adds the current 
    $old_number_of_posts = get_option( "number of published posts", 0);

    // if the option is not set - update it
    // https://codex.wordpress.org/Function_Reference/add_option
    if($old_number_of_posts == 0) {
        add_option( "number of published posts", $new_number_of_posts);
        $old_number_of_posts = $new_number_of_posts;
    }

    if($old_number_of_posts < $new_number_of_posts) {
    unlink(dirname(__FILE__) . "/wp-content/cache/supercache/sitename.com/" . 'index.html.gz');
    }
}

if_new_posts_delete_homepage_cache();

First to answer your question, I'd extract the string "number of published posts" to a constant, something like OPTION_KEY. I'd also make it more concise and database-y, and with a prefix, to prevent collisions. something like 'npdhc:number_posts'.


To answer the question you didn't ask: I don't think count of posts is the best way to make a decision about busting the cache. Theoretically, you could get into edge conditions where you publish one post and delete another and the cache wouldn't get updated.

It's more correct and also simpler to code, to just use the last post's modification time (I think it's $post_modified_gmt but not sure)

Review

Your code is a good hack to clear the cache but it is a hack after all and I won't use it in a production environment.

Reasons for that are -

  1. As others pointed out, your code does not cover the edge cases. If you publish a post and unpublish another post the count remains the same. The cron will not run. In a multi author website this edge case might occur too frequently and your cron will be a hit and miss.
  2. This does not leverage the functionality WordPress provides you to deal with events like post_publish .
  3. Though the effect of a added database hit each time your cron runs might not be too big, but I would prefer to avoid it.

Solution

Now to answer your question, I would use action hooks. https://developer.wordpress.org/reference/functions/add_action/

function clearCacheOnStateChange( $new_status, $old_status, $post ) {
    if ( $new_status != $old_status ) {
        unlink(dirname(__FILE__) . "/wp-content/cache/supercache/sitename.com/" . 'index.html.gz');       
    }
}
add_action(  'transition_post_status',  'clearCacheOnStateChange', 10, 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM