• Resolved Nexxoz

    (@nexxoz)


    I needed to show X posts on the home page with the rate-bar without the ability to rate (just to show) and the ones with high rates, here how i did it. I got some code from the plugins source. This is an simple version of the code i did for one of my wordpress sites.

    
    function wp_post_high_rate($post_type = 'products', $min_votes = 0, $limit = 12, $chars = 0, $display_rate = true){
        if(function_exists('the_ratings')) {
            $i=0;
            global $wpdb;
            $ratings_max = intval(get_option('postratings_max'));
            $ratings_custom = intval(get_option('postratings_customrating'));
            $output = '';
            if(!empty($post_type) && $post_type != 'both') {
              $where = "$wpdb->posts.post_type = '$post_type'";
            } 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 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 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 t2.meta_value >= $min_votes AND $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
    
            if($highest_rated) {
              echo '<div class="row">';
              foreach($highest_rated as $post) {
    
                if($i % 6 ==0) {
                  echo '</div><div class="row">';
                }
    
                echo '<div class="col-xs-6 col-md-2">';
                echo '<div class="thumbnail product-holder">';
                echo '<a href="';
                the_permalink($post->ID); 
                echo '"><img src="'.get_the_post_thumbnail_url($post->ID, 'thumbnail').'" alt="'.$post->post_title.'"></a>';
                echo '<div class="caption text-center">';
                echo '<h4>'.$post->post_title.'</h4>';
                echo '<div class="rate">';
    
                if ($display_rate == true) {
                    echo '<div id="post-ratings-'.$post->ID.'" class="post-ratings">';
                        $score = $post->ratings_score;
                        $off = 5 - $score;
    
                        $y = 1; 
                        while($y <= $score) {
                          echo '<img src="'.home_url().'/wp-content/plugins/wp-postratings/images/stars/rating_on.gif" class="post-ratings-image">';
                          $y++;
                        } 
    
                        $y = 1; 
                        while($y <= $off) {
                          echo '<img src="'.home_url().'/wp-content/plugins/wp-postratings/images/stars/rating_off.gif" class="post-ratings-image">';
                          $y++;
                        } 
                    echo '</div>';
                }
                echo '</div>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
                $i++;
              }
            } else {
              echo __('N/A', 'wp-postratings');
            }
        }
    }
    
  • The topic ‘Display X posts anywhere with rate-bar (cant rate)’ is closed to new replies.