Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 减少Wordpress数据库查询_Php_Sql_Wordpress - Fatal编程技术网

Php 减少Wordpress数据库查询

Php 减少Wordpress数据库查询,php,sql,wordpress,Php,Sql,Wordpress,我的wordpress网站使用平铺格式,在主页上显示10篇文章;一旦你开始滚动,它将加载更多的无限滚动 我正在尝试减少页面加载时间,因为添加此代码后,页面加载时间大约为2.5秒;一秒钟没有它 我想做的是获取每个帖子每小时的社交分享量,并将其显示在主页上。我试图找出一种快速/简单的方法来实现这一点,但似乎这是我知道的唯一方法 因为它显示了10篇文章,所以它必须对每个列出的文章发出2个请求,因此总共有20个请求进入数据库。这对我来说似乎很多,而且似乎有一些简单的方法可以做到这一点,但我却错过了 有什

我的wordpress网站使用平铺格式,在主页上显示10篇文章;一旦你开始滚动,它将加载更多的无限滚动

我正在尝试减少页面加载时间,因为添加此代码后,页面加载时间大约为2.5秒;一秒钟没有它

我想做的是获取每个帖子每小时的社交分享量,并将其显示在主页上。我试图找出一种快速/简单的方法来实现这一点,但似乎这是我知道的唯一方法

因为它显示了10篇文章,所以它必须对每个列出的文章发出2个请求,因此总共有20个请求进入数据库。这对我来说似乎很多,而且似乎有一些简单的方法可以做到这一点,但我却错过了

有什么帮助吗

function getFBShares($postid, $url){

//Grab Results from fb_shares
    $fbcount  = get_post_meta( $postid, 'fb_shares', true );

    if( ! empty( $fbcount ) ) {
        $fbtime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($fbtime < current_time('timestamp') - 60*60){
            $fql = "SELECT share_count FROM link_stat WHERE url = '".$url."'";
            $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode( $fql );
            $json = file_get_contents( $apifql );
            $data = json_decode($json);
            $fbcount = $data[0]->share_count;
            update_post_meta($postid,'fb_shares',$fbcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $fbcount;
        }
        else {
            return $fbcount;
        }

    }
    else {
        //If there is no result, save $fbcount in 'fb_shares' column with current timestamp
        $fql = "SELECT share_count FROM link_stat WHERE url = '".$url."'";
        $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode( $fql );
        $json = file_get_contents( $apifql );
        $data = json_decode($json);
        $fbcount = $data[0]->share_count;
        update_post_meta($postid,'fb_shares',$fbcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $fbcount;
    }
}

function getPlusOne($postid, $url){

    //Grab Results from gp_shares
    $gpcount  = get_post_meta( $postid, 'gp_shares', true );

    if( ! empty( $gpcount ) ) {
        $gbtime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($gbtime < current_time('timestamp') - 60*60){
            $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
            $doc = new DOMDocument();   $doc->loadHTML($html);
            $counter=$doc->getElementById('aggregateCount');
            $gpcount = $counter->nodeValue;
            update_post_meta($postid,'gp_shares',$gpcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $gpcount;
        }
        else {
            return $gpcount;
        }

    }
    else {
        //If there is no result, save $gpcount in 'gp_shares'
        $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
        $doc = new DOMDocument();   $doc->loadHTML($html);
        $counter=$doc->getElementById('aggregateCount');
        $gpcount = $counter->nodeValue;
        update_post_meta($postid,'gp_shares',$gpcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $gpcount;
    }
}

function getMyTweets($postid, $url){

    //Grab Results from social_count
    $tweetcount  = get_post_meta( $postid, 'tweet_shares', true );

    if( ! empty( $tweetcount ) ) {
        $tweettime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($tweettime < current_time('timestamp') - 60*60){
            $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
            $ajsn = json_decode($json, true);
            $tweetcount = $ajsn['count'];
            update_post_meta($postid,'tweet_shares',$tweetcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $tweetcount;
        }
        else {
            return $tweetcount;
        }

    }
    else {
        //If there is no result, save $tweetcount in 'tweet_shares' column with current timestamp
        $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
        $ajsn = json_decode($json, true);
        $tweetcount = $ajsn['count'];
        update_post_meta($postid,'tweet_shares',$tweetcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $tweetcount;
    }
}
函数getFBShares($postid,$url){ //从fb_股票中获取结果 $fbcount=get_post_meta($posted,'fb_shares',true); 如果(!空($fbcount)){ $fbtime=get_post_meta($posted,'share_time',true); //将时间戳与一小时前进行比较 如果($fbtime<当前时间('timestamp')-60*60){ $fql=“从链接统计中选择共享计数,其中url=”$url。“”; $apifql=”https://api.facebook.com/method/fql.query?format=json&query=“.urlencode($fql); $json=文件获取内容($apifql); $data=json_decode($json); $fbcount=$data[0]->共享计数; 更新发布元($postid,'fb\U共享',$fbcount); 更新发布元($postid,'share_time',current_time('timestamp'); 返回$fbcount; } 否则{ 返回$fbcount; } } 否则{ //如果没有结果,请使用当前时间戳在“fb_共享”列中保存$fbcount $fql=“从链接统计中选择共享计数,其中url=”$url。“”; $apifql=”https://api.facebook.com/method/fql.query?format=json&query=“.urlencode($fql); $json=文件获取内容($apifql); $data=json_decode($json); $fbcount=$data[0]->共享计数; 更新发布元($postid,'fb\U共享',$fbcount); 更新发布元($postid,'share_time',current_time('timestamp'); 返回$fbcount; } } 函数getPlusOne($postid,$url){ //从gp_共享中获取结果 $gpcount=get_post_meta($posted,'gp_shares',true); 如果(!空($gpcount)){ $gbtime=get\u post\u meta($posted,'share\u time',true); //将时间戳与一小时前进行比较 如果($gbtime<当前时间('timestamp')-60*60){ $html=文件\u获取\u内容(“https://plusone.google.com/_/+1/fastbutton?url=“.urlencode($url)); $doc=newdomdocument();$doc->loadHTML($html); $counter=$doc->getElementById('aggregateCount'); $gpcount=$counter->nodeValue; 更新发布元($posted,'gp\u shares',$gpcount); 更新发布元($postid,'share_time',current_time('timestamp'); 返回$gpcount; } 否则{ 返回$gpcount; } } 否则{ //如果没有结果,请将$gpcount保存在“gp_共享”中 $html=文件\u获取\u内容(“https://plusone.google.com/_/+1/fastbutton?url=“.urlencode($url)); $doc=newdomdocument();$doc->loadHTML($html); $counter=$doc->getElementById('aggregateCount'); $gpcount=$counter->nodeValue; 更新发布元($posted,'gp\u shares',$gpcount); 更新发布元($postid,'share_time',current_time('timestamp'); 返回$gpcount; } } 函数getMyTweets($postid,$url){ //从社会统计中获取结果 $tweetcount=get_post_meta($posted,'tweet_shares',true); 如果(!empty($tweetcount)){ $tweettime=get_post_meta($posted,'share_time',true); //将时间戳与一小时前进行比较 if($tweettime<当前时间('timestamp')-60*60){ $json=文件\u获取\u内容(“http://urls.api.twitter.com/1/urls/count.json?url=“$url); $ajsn=json_decode($json,true); $tweetcount=$ajsn['count']; 更新发布元($posted,'tweet\u shares',$tweetcount); 更新发布元($postid,'share_time',current_time('timestamp'); 返回$tweetcount; } 否则{ 返回$tweetcount; } } 否则{ //如果没有结果,请使用当前时间戳将$tweetcount保存在“tweet_shares”列中 $json=文件\u获取\u内容(“http://urls.api.twitter.com/1/urls/count.json?url=“$url); $ajsn=json_decode($json,true); $tweetcount=$ajsn['count']; 更新发布元($posted,'tweet\u shares',$tweetcount); 更新发布元($postid,'share_time',current_time('timestamp'); 返回$tweetcount; } }
缓存是否已打开?这将是减少数据库查询的标准方法。目前我没有使用任何缓存插件,我只是通过htaccess缓存图像和脚本。