• Hey all.
    Many times people ask questions like ” How to increase wordpress speed”.

    But i think we can find answers on this questions…

    reduce images, php queries, compress css and java, and other tricks.

    But now.. on my side bar i have 3 hard queries like
    1. Top rated
    2. Most Commented
    3. Most Viewed
    All three queries look in whole posts etc…so they make my sidebar load a bit slow.

    So lets start another part of loading this queries, like making ajax tabs and flecking data from seperated php files where we will include those php queries code`s.

    but how to make separated php files and include those code.

    function for 1. Top rated is

    if(!function_exists('get_highest_rated_range')) {
    	function get_highest_rated_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {
    		global $wpdb, $post;
    		$temp_post = $post;
    		$ratings_max = intval(get_option('postratings_max'));
    		$ratings_custom = intval(get_option('postratings_customrating'));
    		$min_time = strtotime('-'.$time, current_time('timestamp'));
    		$output = '';
    		if(!empty($mode) && $mode != 'both') {
    			$where = "$wpdb->posts.post_type = '$mode'";
    		} else {
    			$where = '1=1';
    		}
    		if($ratings_custom && $ratings_max == 2) {
    			$order_by = 'ratings_score';
    		} else {
    			$order_by = 'ratings_average';
    		}
    		$temp = stripslashes(get_option('postratings_template_highestrated'));
    		$highest_rated = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
    		if($highest_rated) {
    			foreach($highest_rated as $post) {
    				$output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
    			}
    		} else {
    			$output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
    		}
    		$post = $temp_post;
    		if($display) {
    			echo $output;
    		} else {
    			return $output;
    		}
    	}
    }

    code for 2.most commented is

    <?php
    $results = $wpdb->get_results("SELECT ID, comment_count FROM wp_posts WHERE post_type = 'post' && post_status = 'publish' ORDER BY comment_count DESC LIMIT 10");
    foreach ($results as $r):
        query_posts(array('p' => $r->ID));
        while (have_posts()):
          the_post();
    ?>
    <ul>
    <li style="display:block; list-style:none"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a> (<?php comments_number('0', '1', '%'); ?>)</li>
    </ul>
    <?php endwhile; endforeach; ?>

    Function for most viewed is

    if(!function_exists('get_most_viewed')) {
    	function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
    		global $wpdb;
    		$views_options = get_option('views_options');
    		$where = '';
    		$temp = '';
    		$output = '';
    		if(!empty($mode) && $mode != 'both') {
    			$where = "post_type = '$mode'";
    		} else {
    			$where = '1=1';
    		}
    		$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");
    		if($most_viewed) {
    			foreach ($most_viewed as $post) {
    				$post_views = intval($post->views);
    				$post_title = get_the_title($post);
    				if($chars > 0) {
    					$post_title = snippet_text($post_title, $chars);
    				}
    				$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
    				$temp = stripslashes($views_options['most_viewed_template']);
    				$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp);
    				$temp = str_replace("%POST_TITLE%", $post_title, $temp);
    				$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
    				$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp);
    				$temp = str_replace("%POST_URL%", get_permalink($post), $temp);
    				$output .= $temp;
    			}
    		} else {
    			$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
    		}
    		if($display) {
    			echo $output;
    		} else {
    			return $output;
    		}
    	}
    }

    p.s i know how to make ajax tabs for getting external data from another php file…

    any1 with some idea?

  • The topic ‘Increase speed by logical ajax tabs queries’ is closed to new replies.