我仍然只处于 MySQL 的初学者阶段,并且想知道如何为此添加特定类别。
SELECT DISTINCT DATE_FORMAT(post_date, '%b') AS month ,
MONTH(post_date) as numMonth, YEAR( post_date ) AS year,
COUNT( id ) as post_count
FROM $wpdb->posts
WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post'
GROUP BY month , year ORDER BY post_date DESC
我发现此代码用于获取类别uncategorized
...
SELECT * FROM wp_posts p
LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id
WHERE p.post_status = 'publish' AND p.post_type = 'post'
AND t.slug = 'uncategorized'
我试过UNION ALL
了,但这阻止了任何东西的出现。如果我试图学习这些东西,也许我走得太远了,因为我什至无法让查询的第二部分自行工作。
谢谢
编辑:作为对第一个响应的回应,请添加此代码以进一步解释我在做什么,它是一个档案小部件,每年然后每月显示。我还没有找到一种简单的方法来做到这一点,并求助于“更好的档案小部件”。这是他们已包含在我希望更改的文件中的代码。如果有使用query_posts
Id 的更简单方法,请对此持开放态度。
global $wpdb;
$prevYear = "";
$currentYear = "";
if ( $months = $wpdb->get_results( "SELECT DISTINCT DATE_FORMAT(post_date, '%b') AS month , MONTH(post_date) as numMonth, YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC" ) ) {
echo '<ul>';
$count = 0;
foreach ( $months as $month ) {
$currentYear = $month->year;
if ( ( $currentYear != $prevYear ) && ( $prevYear != "" ) ) { echo "</ul></li>"; }
if ( $currentYear != $prevYear ) {
$count++ ?>
<li class="baw-year"><a href="<?php echo get_year_link( $month->year ); ?>"><?php echo $month->year; echo ' ('.$month->post_count . ')' ; ?> </a>
<ul class="baw-months">
<?php
} ?>
<li class="baw-month"><a href="<?php echo get_month_link( $month->year, $month->numMonth ); ?>"><?php echo date('F', strtotime ($month->month)); ?><?php /*echo ' ' . $month->year; */ ?></a></li>
<?php
$prevYear = $month->year;
}//end foreach
}
?>
</ul></li>