Advertisement
Guest User

Dominykas

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