Advertisement
Guest User

Domas

a guest
Mar 31st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. /**
  2.  * A custom shortcode to display top rated posts.
  3.  *
  4. */
  5. function elm_custom_top_rated( $atts ) {
  6.  
  7.     $atts = shortcode_atts( array(
  8.         'post_type' => '',
  9.         'sort' => 'asc',
  10.         'limit' => ''
  11.     ), $atts );
  12.            
  13.     global $wpdb;
  14.  
  15.     // LIMIT posts
  16.     $atts['limit'] = 10;
  17.            
  18.     if ( !$atts['post_type'] ) {
  19.         $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings";
  20.                
  21.         if ( $atts['limit'] )
  22.             $sql .= " LIMIT {$atts['limit']}";
  23.                
  24.             $get_posts = $wpdb->get_results( $sql );
  25.     } else {
  26.         $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings WHERE type = '{$atts['post_type']}'";
  27.            
  28.         if ( $atts['limit'] )
  29.             $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings WHERE type = '{$atts['post_type']}' LIMIT {$atts['limit']}";
  30.            
  31.         $get_posts = $wpdb->get_results( $sql );
  32.     }
  33.        
  34.         if ( empty( $get_posts ) )
  35.             return;
  36.        
  37.         foreach ( $get_posts as $k => $post ) {
  38.             $average = intval( get_post_meta( $post->post_id, '_average_page_rating', TRUE ) );
  39.            
  40.             $posts[$k]['id']             = $post->post_id;
  41.             $posts[$k]['average_rating'] = $average;
  42.         }
  43.        
  44.         foreach ( $posts as $k => $v ) {
  45.             $b[$k] = intval( $v['average_rating'] );
  46.         }
  47.        
  48.         // Sort
  49.         if ( $atts['sort'] == 'asc' ) {
  50.             arsort( $b );
  51.         } else {
  52.             asort( $b );
  53.         }
  54.        
  55.         $html = '';
  56.        
  57.         // HTML output
  58.         if ( $b ) {            
  59.             foreach ( $b as $key => $val ) {
  60.                 $html .= '<div>';
  61.                
  62.                 if ( has_post_thumbnail($posts[$key]['id']) ) {
  63.                     $html .= '<div class="thumbnail">' . get_the_post_thumbnail( $posts[$key]['id'] ) . '</div>';
  64.                 }
  65.  
  66.                 $html .= '<h2 class="title entry-title">'. get_the_title( $posts[$key]['id'] ) .'</h2>';
  67.                 $html .= apply_filters('the_excerpt', get_post_field( 'post_content', $posts[$key]['id'] ) );
  68.                 $html .= '<span class="viewmore"><a href="'. get_permalink( $posts[$key]['id'] ) .'">'. __('VIEW MORE') .'</a></span>';
  69.                 $html .= '<div class="clear"></div>';
  70.                 $html .= '<hr />';
  71.  
  72.                 $html .= '</div>';
  73.             }
  74.         }
  75.        
  76.     return $html;
  77.  }
  78.  
  79. add_shortcode( 'elm_custom_top_rated', 'elm_custom_top_rated' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement