Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A custom shortcode to display top rated posts.
- *
- */
- function elm_custom_top_rated( $atts ) {
- $atts = shortcode_atts( array(
- 'post_type' => '',
- 'sort' => 'asc',
- 'limit' => ''
- ), $atts );
- global $wpdb;
- if ( !$atts['post_type'] ) {
- $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings";
- if ( $atts['limit'] )
- $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings WHERE LIMIT {$atts['limit']}";
- $get_posts = $wpdb->get_results( $sql );
- } else {
- $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings WHERE type = '{$atts['post_type']}'";
- if ( $atts['limit'] )
- $sql = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}elm_ratings WHERE type = '{$atts['post_type']}' LIMIT {$atts['limit']}";
- $get_posts = $wpdb->get_results( $sql );
- }
- if ( empty( $get_posts ) )
- return;
- foreach ( $get_posts as $k => $post ) {
- $average = intval( get_post_meta( $post->post_id, '_average_page_rating', TRUE ) );
- $posts[$k]['id'] = $post->post_id;
- $posts[$k]['average_rating'] = $average;
- }
- foreach ( $posts as $k => $v ) {
- $b[$k] = intval( $v['average_rating'] );
- }
- // Sort
- if ( $atts['sort'] == 'asc' ) {
- arsort( $b );
- } else {
- asort( $b );
- }
- $html = '';
- // HTML output
- if ( $b ) {
- foreach ( $b as $key => $val ) {
- $html .= '<div>';
- if ( has_post_thumbnail($posts[$key]['id']) ) {
- $html .= '<div class="thumbnail">' . get_the_post_thumbnail( $posts[$key]['id'] ) . '</div>';
- }
- $html .= '<h2 class="title entry-title">'. get_the_title( $posts[$key]['id'] ) .'</h2>';
- $html .= apply_filters('the_excerpt', get_post_field( 'post_content', $posts[$key]['id'] ) );
- $html .= '<span class="viewmore"><a href="'. get_permalink( $posts[$key]['id'] ) .'">'. __('VIEW MORE') .'</a></span>';
- $html .= '<div class="clear"></div>';
- $html .= '<hr />';
- $html .= '</div>';
- }
- }
- return $html;
- }
- add_shortcode( 'elm_custom_top_rated', 'elm_custom_top_rated' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement