• How strong will be server load, if I use this code?

    function get_posts_count_from_last_24h($post_type ='post') {
        global $wpdb;
    
        $numposts = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(ID) ".
                "FROM {$wpdb->posts} ".
                "WHERE ".
                    "post_status='publish' ".
                    "AND post_type= %s ".
                    "AND post_date> %s",
                $post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
            )
        );
        return $numposts;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • It entirely depends on your server config and the amount of traffic you get. If you have high configuration and low traffic and small database size, it won’t be an issue.

    Thread Starter Pacman

    (@drunkowl)

    What if I do this? This code will work? oO

    function get_posts_count_from_last_24h($post_type ='post') {
    
        $cached = get_transient( 'np_count' );
        if ( $cached !== false )
            return $cached;
    
        global $wpdb;
    
        $numposts = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(ID) ".
                "FROM {$wpdb->posts} ".
                "WHERE ".
                    "post_status='publish' ".
                    "AND post_type= %s ".
                    "AND post_date> %s",
                $post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
            )
        );
    
        set_transient( 'np_count', $numposts, 1 * HOUR_IN_SECONDS );
    
        return $numposts;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Server load’ is closed to new replies.