我正在创建一个基于某些选项显示数据的短代码。
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\');
然后,短代码如下:
<?php echo do_shortcode("[irt_top_posts post_type=\'showcase\' number=\'10\' monthnum=\'9\']"); ?>
UPDATED QUESTION\'s
<我的查询似乎有问题。法典
http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters 有一个使用WP\\U查询时使用日期检索结果的示例。我想知道在获取结果时,我需要对代码进行哪些更改才能在查询中包含年份或月份。我注意到,在与数据库通信时,大多数短代码都使用wpdb类而不是wp\\U查询。这背后有什么原因吗
SO网友:Harish Chouhan
终于从另一个帖子得到了答案Custom $wpdb Query for Custom Post Type by Category:
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(post_date) = \'$year\'";
}
if ($monthnum != \'\') {
$request .= " AND MONTH(post_date) = \'$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\');