I am creating a shortcode to display data based on some options.
function dot_irt_top_posts ( $atts, $content = null ) {
// get our variable from $atts
extract(shortcode_atts(array(
'before' => '<li>',
'after' => '</li>',
'number' => '10',
'post_type' => 'post',
'year' => '',
'monthnum' => '',
'show_count' => '1',
), $atts));
global $wpdb;
$request = "SELECT * FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
if ($year != '') {
$request .= " AND year=$year";
}
if ($year != '' && $monthnum != '') {
$request .= " AND &monthnum='$monthnum'";
} elseif ($monthnum != '') {
$request .= " AND monthnum='$monthnum'";
}
$request .= " AND post_status='publish' AND post_type='$post_type' AND meta_key='_recommended'";
$request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $number";
$posts = $wpdb->get_results($request);
$return = '';
foreach ($posts as $item) {
$post_title = stripslashes($item->post_title);
$permalink = get_permalink($item->ID);
$post_count = $item->meta_value;
$return .= $before.'<a href="' . $permalink . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a> ';
if ( $show_count == '1') {
$return .= '<span class="votes">' . $post_count . '</span> ';
}
//$return .= get_the_post_thumbnail($item->ID, 'showcase-thumbnail');
$return .= $after;
}
return $return;
}
add_shortcode('irt_top_posts','dot_irt_top_posts');
And then the shortcode would be like:
<?php echo do_shortcode("[irt_top_posts post_type='showcase' number='10' monthnum='9']"); ?>
UPDATED QUESTION's
- There seems to be some issue with my query. The Codex http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters has an example for using the date to retrive results when using WP_Query. I want to know what changes I need to make to my code to have YEAR or MONTH in the query when getting results
- I noticed most shortcodes when communicating with database use wpdb class and not wp_query. Is there a reason behind this?
$wpdb->last_error
and$wpdb->last_query
as edit to your question. Also your MarkUp has problems. Please edit your question and fix that.