我正在尝试创建一个插件,用于显示特定日期范围和类别的帖子标题和日期(将显示为这些帖子的链接)。到目前为止,我已经设法在两个日期之间显示帖子,但我无法对WP类别的事情保持清醒。我不知道如何将这两者连接到一个SQL查询中。以下是迄今为止的代码:
function izvjestaj($attr) {
global $wpdb;
$start .= $attr[\'godina\'] . "-09-01";
$end .= $attr[\'godina\'] + 1 . "-08-31";
$cat = $attr[\'cat\'];
$posts = $wpdb->get_results("
SELECT *
FROM wp_posts, wp_terms
WHERE post_date
BETWEEN \'$start\' AND \'$end\'
ORDER BY post_date");
foreach($posts as $post):
echo "<a href=\'./?p=" . $post->ID . "\'>" . $post->post_date . " " . $post->post_title . "</a><br />";
endforeach;
}
add_shortcode(\'izvjestaj\', \'izvjestaj\');
因此,基本上我想创建一个可以与短代码一起使用的插件:
[izvjestaj godina=2010 cat=some_category]
并显示这些帖子的标题和日期。基本上是一个职位报告。
(翻译:cro>eng:“godina”=“year”,“izvjestaj”=“report”)
最合适的回答,由SO网友:Domain 整理而成
请尝试以下代码:
必须在shortcode函数中添加以下代码
global $wpdb;
$start .= $attr[\'godina\'] . "-09-01";
$end .= $attr[\'godina\'] + 1 . "-08-31";
$cat = $attr[\'cat\']; //$cat must be category name.
$cat_term_id=get_term_by(\'name\', $cat, \'category\');
$term_id=$cat_term_id->term_id;
$query = array(
\'post_type\' => \'post\',
\'tax_query\' => array(array( \'taxonomy\' => \'category\',
\'field\' => \'term_id\',
\'terms\'=>$term_id
)),
\'post_status\' => \'publish\',
\'date_query\' => array(
array(
\'after\' => $start,
\'before\' => $end,
\'inclusive\' => true,
)
)
);
$query_str = new WP_Query( $query );