我有这个功能:
$ids = $wpdb->get_col("SELECT DISTINCT comment_post_ID
FROM $wpdb->comments
ORDER BY comment_date DESC
LIMIT 0 , 30");
foreach ($ids as $id) {
$post = &get_post( $id );
setup_postdata($post); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
}
?>
在列表中显示latest commented posts
,这很好。我想要做的是优先考虑这个并将其与“get newest post list
”结合起来。所以,假设我今天在一个名为Hello World的帖子上发表了评论,其他人昨天提交了一篇帖子......我希望得到这篇新帖子上方最近评论的帖子。问题是,在我的代码片段中,没有任何内容可以说明最新的帖子。我如何合并他们? 那么如何将最近评论的帖子和最新帖子相互结合?这甚至可能吗?
答案 0 :(得分:0)
试一试最适合我的工作查询使用left jon
和comments
表获取所有帖子,这样当帖子评论时他们= n它还有{{ 1}}如果帖子上没有发布评论,那么在结果集中它将是comment_date
所以我已将null
与comment_date
合并,以便哪个帖子的日期更长(对于comment_date或post_date )它将首先等等
post_date
要显示帖子,您必须首先通过定义WP的数据库互动全局变量来获得结果,即SELECT p.*,
(CASE WHEN c.comment_date IS NULL THEN p.`post_date` ELSE c.comment_date END) order_column
FROM `wp_posts` p
LEFT JOIN `wp_comments` c ON (p.ID = c.`comment_post_ID` ) WHERE p.post_type='post' AND p.post_status='publish'
GROUP BY p.ID
ORDER BY order_column DESC
$wpdb
HTML
<?php
global $wpdb;
$results = $wpdb->get_results(" SELECT p.*,
(CASE WHEN c.comment_date IS NULL THEN p.`post_date` ELSE c.comment_date END) order_column
FROM `wp_posts` p
LEFT JOIN `wp_comments` c ON (p.ID = c.`comment_post_ID` ) WHERE p.post_type='post' AND p.post_status='publish'
GROUP BY p.ID
ORDER BY order_column DESC");
?>
希望这就是你要找的东西