• I’m using a ‘top rated posts’ plugin [‘Post Ratings’ by Digitalnature] for my wordpress installation, which allows users to vote on posts using a 5 star rating system.

    The plugin then allows you to pull the top rated posts in to your template files so that you can create a page listing the ‘Top 20’ for example.

    The readme file that comes with the plugin has the following example code:

    = How do I manually get the most rated posts using PHP? =
    
          <code>$posts = PostRatings()->getTopRated($arguments);</code>
    
        Possible arguments, and their defaults:
    
            'post_type' => 'post',
            'number'       => 10,                   // max. number of posts to retrieve
            'offset'       => 0,                    // offset from where to start
            'sortby'       => 'bayesian_rating',    // bayesian_rating, rating or votes
            'order'        => 'DESC',               // ASC or DESC
            'date_limit'   => 0,                    // date limit in days
    
        Return value is an array of post objects containing the usual post properties + 3 extra properties:
    
            $post->votes                            // number of votes
            $post->rating                           // average rating
            $post->bayesian_rating                  // weighted rating
    
        Example usage:
    
            global $post;
            $results = PostRatings()->getTopRated();
    
            foreach($results as $post){
              setup_postdata($post); ?>
    
              // here is the usual loop
              <?php the_title(); ?> - <?php printf(_n('%d vote', '%d votes', $post->votes), $post->votes); ?>
    
              <?php
            }
    
            // restore original post
            wp_reset_postdata();

    My standard Page template file that i’m trying to integrate this with is as follows:

    /*---------------------------------
        	Template Name: Top Rated Mixtapes
        ------------------------------------*/
    
        	get_header();
        	$k1 = 0;
        	$k2 = 0;
    
        ?>
        		<section id="portfolio" class="folioGrid clearfix">
        			<?php while (have_posts()) : the_post(); 
    
        				$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    
        				$args = array( 'posts_per_page' => 20,
        					   'offset'=> 0,
        					   'post_type' => 'portfolio');
    
        				$all_posts = new WP_Query($args);
    
        				while($all_posts->have_posts()) : $all_posts->the_post();
    
        				$portfolio_terms = wp_get_object_terms($post->ID, 'portfolio_category');
        				$portfolio_class = "folioItem " . $portfolio_terms[0]->slug;
        				$portfolio_sort = $portfolio_terms[0]->slug . '[1][0]';
        				$portfolio_type = $portfolio_terms[0]->slug;
    
        			?>
    
        			<a>" class="<?php echo $portfolio_class; ?>" data-sort="<?php echo $portfolio_sort; ?>" data-type="<?php echo $portfolio_type; ?>" href="<?php the_permalink(); ?>" data-name="<?php echo $post->post_name; ?>" data-external="<?php echo get_post_meta($post->ID, 'rb_post_url_d', true) != '' ? get_post_meta($post->ID, 'rb_post_url_d', true) : 'no'; ?>">
    
        				<?php the_post_thumbnail('portfolio-thumb', array('class' => 'folioThumb'));?>
    
        				<div class="folioTextHolder">
        					<div class="folioText">
        						<h3><?php the_title(); ?></h3>
        						<p><?php rb_excerpt('rb_excerptlength_folio', 'rb_excerptmore'); ?></p>
        					</div>
        				</div>
    
        			</a>
        			<?php endwhile; ?>
        		<?php endwhile; ?>

    What i can’t seem to understand is how to implement it.
    Any advice appreciated.

  • The topic ‘WordPress Top Rated Posts Template’ is closed to new replies.