Skip to content

Instantly share code, notes, and snippets.

@hchouhan
Last active August 29, 2015 14:06
Show Gist options
  • Save hchouhan/128c7cf6a699210fec99 to your computer and use it in GitHub Desktop.
Save hchouhan/128c7cf6a699210fec99 to your computer and use it in GitHub Desktop.
<?php
add_shortcode( 'dot_recommended_posts', array( &$this, 'dot_recommended_top_posts' ) );
function dot_recommended_top_posts( $atts, $content = null )
{
// get our variable from $atts
$atts = shortcode_atts( array(
'container' => 'li',
'number' => '10',
'post_type' => 'post',
'year' => '',
'monthnum' => '',
'show_count' => '1',
), $atts );
global $wpdb;
// empty params array to hold params for prepared statement
$params = array();
// build query string
$sql = "SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
// add year
if( '' !== $atts['year'] ) {
$sql .= ' AND YEAR(post_date) = %d';
$params[] = $atts['year'];
}
// add monthnum
if( '' !== $atts['monthnum'] ) {
$sql .= ' AND MONTH(post_date) = %d';
$params[] = $atts['monthnum'];
}
// add post WHERE
$sql .= " AND post_status = 'publish' AND post_type = %s AND meta_key = '_recommended'";
$params[] = $atts['post_type'];
// add order by and limit
$sql .= " ORDER BY {$wpdb->postmeta}.meta_value+0 DESC LIMIT %d";
$params[] = $atts['number'];
// prepare sql statement
$query = $wpdb->prepare( $sql, $params );
// execute query
$posts = $wpdb->get_results( $query );
$return = '';
foreach ($posts as $item) {
$post_title = stripslashes( $item->post_title );
$permalink = get_permalink( $item->ID );
$post_count = $item->meta_value;
$return .= '<' . esc_html( $atts['container'] ) . '>';
$return .= '<a href="' . esc_url( $permalink ) . '" title="' . esc_attr( $post_title ) .'" rel="nofollow">' . esc_html( $post_title ) . '</a> ';
if ( $atts['show_count'] == '1') {
$return .= '<span class="votes">' . esc_html( $post_count ) . '</span> ';
}
//$return .= get_the_post_thumbnail($item->ID, 'showcase-thumbnail');
$return .= '</' . esc_html( $atts['container'] ) . '>';
}
return $return;
} //dot_recommended_top_posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment