Lester Chan's Forums
07 September 2008, 13:58 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Lester Chan’s WordPress Plugins July 2008 Update
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: get_highest_rated_category with pagination  (Read 2845 times)
0 Members and 1 Guest are viewing this topic.
dirs
Newbie
*
Offline Offline

Posts: 5


View Profile
« Topic Start: 22 August 2007, 06:52 »

Hi!
Today,
My client say: "this top-100 most ranked photos can be broken in pages of 25 photos?"
And I say: "yeah, but will spend a little of time... maybe us can transform it in top-25 and don't have to paginate"
He: "NO! PAGINATE"

soooo, i paginate Smiley

I will show how to paginate the Highest rated with a "Digg Style" pagination - FROM: *WP-Digg Style Paginator ( http://www.mis-algoritmos.com)

IMPORTANT: this is not a official part of original code of WP-PostRatings Plugin, this is only for show what is possible and help anyone who need that for the projects.

Code:
  function get_highest_rated_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
    global $wpdb, $post, $paged;
    $ratings_image = get_option('postratings_image');
    $ratings_max = intval(get_option('postratings_max'));
    $ratings_custom = intval(get_option('postratings_customrating'));
    $where = '';
    $temp = '';
    $output = '';
    //remove the POG of 1=1 =)
    if(!empty($mode) && $mode != 'both') {
      $where = "AND $wpdb->posts.post_type = '$mode'";
    }
    if($ratings_custom && $ratings_max == 2) {
      $order_by = 'ratings_score';
    } else {
      $order_by = 'ratings_average';
    }
    //for show multiples categories (see more in: http://forums.lesterchan.net/index.php/topic,971.0.html)
    if(is_array($category_id)) {
      $cat_filter = "IN (".join(",", $category_id).")";
    } else {
      $cat_filter = "= $category_id";
    }
    // for paginate (counts results for can define $total_pages, etc...
    $sqlStr = "SELECT count(*) as A FROM (SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta AS t2 ON t1.post_id = t2.post_id LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->post2cat ON $wpdb->post2cat.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->post2cat.category_id $cat_filter $where) as B";
   
    //FROM *WP-Digg Style Paginator (http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/ - with some mods)
    $total_pages = $wpdb->get_var($sqlStr);//total number of rows in data table
    if(!empty($paged))$page = $paged; else $page = 1;
    if($page) {
      $start = ($page - 1) * $limit;
    } else {
      $start = 0;
    }
    if ($page == 0) $page = 1;
    $prev = $page - 1;
    $siguiente = $page + 1;
    $lastpage = ceil($total_pages/$limit);
    $lpm1 = $lastpage - 1;
    $limit = $start.', '.$limit;
    //
   
    $highest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta AS t2 ON t1.post_id = t2.post_id LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->post2cat ON $wpdb->post2cat.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->post2cat.category_id $cat_filter $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
    if($highest_rated) {
      foreach($highest_rated as $post) {
        // Variables
        $post_ratings_users = $post->ratings_users;
        $post_ratings_images = '';
        $post_title = get_the_title();
        $post_ratings_average = $post->ratings_average;
        $post_ratings_score = $post->ratings_score;
        $post_ratings_whole = intval($post_ratings_average);
        $post_ratings = floor($post_ratings_average);
        $post_excerpt = stripslashes($post->post_excerpt);
        $post_content = stripslashes($post->post_content);
        // Check For Half Star
        $insert_half = 0;
        $average_diff = $post_ratings_average-$post_ratings_whole;
        if($average_diff >= 0.25 && $average_diff <= 0.75) {
          $insert_half = $post_ratings_whole+1;
        } elseif($average_diff > 0.75) {
          $post_ratings = $post_ratings+1;
        }
        if($ratings_custom && $ratings_max == 2) {
          if($post_ratings_score > 0) {
            $post_ratings_score = '+'.$post_ratings_score;
          }
          $image_alt = $post_ratings_score.' '.__('rating', 'wp-postratings');
        } else {
          $image_alt = $post_ratings_users.' '.__('votes', 'wp-postratings').', '.__('average', 'wp-postratings').': '.$post_ratings_average.' '.__('out of', 'wp-postratings').' '.$ratings_max;
        }
        // Display Start Of Rating Image
        if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_start.gif')) {
          $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_start.gif" alt="" class="post-ratings-image" />';
        }
        if(!$ratings_custom) {
          for($i=1; $i <= $ratings_max; $i++) {
            if($i <= $post_ratings) {
              $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_on.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
            } elseif($i == $insert_half) {
              $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_half.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
            } else {
              $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_off.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
            }
          }
        } else {
          for($i=1; $i <= $ratings_max; $i++) {
            if($i <= $post_ratings) {
              $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_'.$i.'_on.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
            } elseif($i == $insert_half) {
              $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_'.$i.'_half.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
            } else {
              $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_'.$i.'_off.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
            }
          }
        }
        // Display End Of Rating Image
        if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_end.gif')) {
          $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_end.gif" alt="" class="post-ratings-image" />';
        }
        if($chars > 0) {
          if($ratings_custom && $ratings_max == 2) {
            $temp = "<li><a href=\"".get_permalink()."\">".snippet_chars($post_title, $chars)."</a> ".$image_alt."</li>\n";
          } else {
            $temp = "<li><a href=\"".get_permalink()."\">".snippet_chars($post_title, $chars)."</a> ".$post_ratings_images."</li>\n";
          }
        } else {
          // Display The Contents
          $temp = stripslashes(get_option('postratings_template_highestrated'));
          $temp = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $temp);
          $temp = str_replace("%RATINGS_MAX%", $ratings_max, $temp);
          $temp = str_replace("%RATINGS_AVERAGE%", $post_ratings_average, $temp);
          $temp = str_replace("%RATINGS_SCORE%", $post_ratings_score, $temp);
          $temp = str_replace("%RATINGS_USERS%", number_format($post_ratings_users), $temp);
          $temp = str_replace("%POST_TITLE%", $post_title, $temp);
          $temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
          $temp = str_replace("%POST_CONTENT%", $post_content, $temp);
          $temp = str_replace("%POST_URL%", get_permalink(), $temp);
        }
        $output .= $temp;
      }
    } else {
      $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
    }
    if($display) {
      echo $output;
      echo_pagination($lastpage,$page,$prev,$adjacents,$counter,$lpm1,$siguiente);
    } else {
      return $output;
    }
  }

Note, is simple! I only use a part of *WP-Digg Style Paginator and call the echo_pagination function to write the paginate html

code of echo_pagination():

Code:
function echo_pagination($lastpage,$page,$prev,$adjacents,$counter,$lpm1,$next) {
  if($lastpage > 1){
    //previous button
    if($page > 1)
        echo "<a href=\"".get_pagenum_link($prev)."\">Previous</a>";
      else
        echo "<span class=\"disabled\">Previous</span>";
    //pages
    if ($lastpage < 7 + ($adjacents * 2)){//not enough pages to bother breaking it up
        for ($counter = 1; $counter <= $lastpage; $counter++){
            if ($counter == $page)
                echo "<span class=\"current\">$counter</span>";
              else
                echo "<a href=\"".get_pagenum_link($counter)."\">$counter</a>";
          }
      }
    elseif($lastpage > 5 + ($adjacents * 2)){//enough pages to hide some
        //close to beginning; only hide later pages
        if($page < 1 + ($adjacents * 2)){
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                if ($counter == $page)
                    echo "<span class=\"current\">$counter</span>";
                  else
                    echo "<a href=\"".get_pagenum_link($counter)."\">$counter</a>";
              }
            echo "...";
            echo "<a href=\"".get_pagenum_link($lpm1)."\">$lpm1</a>";
            echo "<a href=\"".get_pagenum_link($lastpage)."\">$lastpage</a>";
          }
        //in middle; hide some front and some back
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){
            echo "<a href=\"".get_pagenum_link(1)."\">1</a>";
            echo "<a href=\"".get_pagenum_link(2)."\">2</a>";
            echo "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
              if ($counter == $page)
                  echo "<span class=\"current\">$counter</span>";
                else
                  echo "<a href=\"".get_pagenum_link($counter)."\">$counter</a>";
            echo "...";
            echo "<a href=\"".get_pagenum_link($lpm1)."\">$lpm1</a>";
            echo "<a href=\"".get_pagenum_link($lastpage)."\">$lastpage</a>";
          }
        //close to end; only hide early pages
        else{
            echo "<a href=\"".get_pagenum_link(1)."\">1</a>";
            echo "<a href=\"".get_pagenum_link(2)."\">2</a>";
            echo "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
              if ($counter == $page)
                  echo "<span class=\"current\">$counter</span>";
                else
                  echo "<a href=\"".get_pagenum_link($counter)."\">$counter</a>";
          }
      }
    //next button
    if ($page < $counter - 1)
        echo "<a href=\"".get_pagenum_link($next)."\">Next</a>";
      else
        echo "<span class=\"disabled\">Next</span>";
  }
}

IMPORTANT!!! 
For pagination can work, unhappyly i need to hack a little the WordPress in the file "wp-includes/classes.php" (2.2.2)
on function handle_404() have this code:

Code:
if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) )) ) {

So, if the original query return no posts the WP will give a 404 error.
And probably you will call get_highest_rated_category from a Category Page resulting on 404 error when:
Don't have any post on the category or if go to a "page 2" of pagination.

For resolve it, i put a one more parameter in the if of handle_404() for check if is the speciffic category you want show the "Highest_Rated".

Can u use this hack too, is like this:

Code:
if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) && !(is_category(N)) ) {

TIP: Maybe the Devs of WP-PostRatings Plugin and *WP-Digg Style Paginator can talk and make two plugins work together.
In my case, to DRY the code i don't create a echo_pagination in WP-PostRatings Plugin code. I've created this on the code of *WP-Digg Style Paginator, and make the two call this function to write the pagination.

WOW! Too Much Code, but can help someone and this is my re-tribuition for WP-PostRatings creator =)
(and sorry about my nonsense of verbal timing, I'm a portuguese guy so don't have good skill in english)

http://pomoti.com is my URL, so when someone have a question can send me a comment on any post Smiley
Logged
GaMerZ
lesterchan.net
Administrator
*****
Offline Offline

Posts: 7,089



View Profile WWW
« Reply #1: 22 August 2007, 09:00 »

erm

Or another similar way is to uncomment
Code:
/*
if($_GET['sortby'] == 'most_rated') {
add_filter('posts_fields', 'ratings_most_fields');
add_filter('posts_join', 'ratings_most_join');
add_filter('posts_where', 'ratings_most_where');
add_filter('posts_orderby', 'ratings_most_orderby');
} elseif($_GET['sortby'] == 'highest_rated') {
add_filter('posts_fields', 'ratings_highest_fields');
add_filter('posts_join', 'ratings_highest_join');
add_filter('posts_where', 'ratings_highest_where');
add_filter('posts_orderby', 'ratings_highest_orderby');
}
*/
in postratings.php

Then

your URL: http://yousite.com/blog/category/?sortby=highest_rated

If you are using digg style pagination or my wp-pagenavi, it should paginate like normal post.
Logged


++ lesterchan.net - Lester Chan's Website

I DO NOT provide support for any modifications of any plugin to meet your needs/requirements, unless it is a paid job and I have the time to do it due to the large number of request.
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,290



View Profile WWW
« Reply #2: 22 August 2007, 20:07 »

Heh... Interesting... I didn't know this...
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
dirs
Newbie
*
Offline Offline

Posts: 5


View Profile
« Reply #3: 22 August 2007, 20:12 »

erm

Or another similar way is to uncomment
Code:
/*
if($_GET['sortby'] == 'most_rated') {
add_filter('posts_fields', 'ratings_most_fields');
add_filter('posts_join', 'ratings_most_join');
add_filter('posts_where', 'ratings_most_where');
add_filter('posts_orderby', 'ratings_most_orderby');
} elseif($_GET['sortby'] == 'highest_rated') {
add_filter('posts_fields', 'ratings_highest_fields');
add_filter('posts_join', 'ratings_highest_join');
add_filter('posts_where', 'ratings_highest_where');
add_filter('posts_orderby', 'ratings_highest_orderby');
}
*/
in postratings.php

Then

your URL: http://yousite.com/blog/category/?sortby=highest_rated

If you are using digg style pagination or my wp-pagenavi, it should paginate like normal post.

yeah? nice Smiley thanks
Logged
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,290



View Profile WWW
« Reply #4: 22 August 2007, 20:20 »

Smiley
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.048 seconds with 23 queries.