如何获得最新评论的帖子上面新提交的帖子在Wordpress? [英] How to get most recent commented post above new submitted post in Wordpress?

查看:128
本文介绍了如何获得最新评论的帖子上面新提交的帖子在Wordpress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数:

  $ 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 =bookmarktitle =永久链接到<?php the_title_attribute();?>><?php the_title ?>< / a>< / p>
<?php
}
?>

其中显示了最新评论的帖子列表,这是罚款。我想要做的是给这个优先级,并结合它与一个获得最新的帖子列表。所以,让我们说,我今天评论一个名为Hello World的帖子,有人提交了一个帖子昨天...比我想获得最近评论的帖子上面这个新的职位。问题是,在我的代码片段,没有说,获得最新的帖子。如何组合那么如何将最近发表的评论帖子和最新帖子结合起来呢?这是可能吗?

解决方案

p>给它一个尝试工作完美的我正在做什么查询获取所有的帖子与 left jon 评论表,所以当一个帖子评论他们= n它还有 comment_date 如果没有评论张贴在帖子,然后在结果集将它 null 所以我已经合并了 comment_date post_date 所以哪个帖子有更大的日期对于comment_date或post_date),它将首先等等。

  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 pp.ost_status ='publish'
GROUP BY p.ID
ORDER BY order_column DESC

为了显示帖子,你必须首先通过定义WP的全局变量为数据库交互获得结果, $ wpdb

 <?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 `$ 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);
?>



HTML

 <?php foreach($ results as $ result){

< h1><?php echo $ result-> post_title;?>< / h1&
< div> <?php echo $ result-> post_content;?> < / div>
//等等字段的wp_posts
<?php} // loop end?>

希望这是您正在寻找的


I have this function:

$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
}
?>

Which shows the latest commented posts in a list, which is fine. What I want to do is give a priority to this one and combine it with a "get newest post list". So let's say I commented today on a post called Hello World and someone else submitted a post yesterday... Than I want to get the recent commented post above this new post. The problem is that in my code snippet, there is nothing that says to get the newest posts. How can I combine them? So how to combine most recent commented posts and newest posts with each other? Is this even possible?

解决方案

Give it a try works perfect for me what it is doing query get the all the posts with a left jon with comments table so when a post has comment them=n it also has the comment_date if no comments posted on the post then in result set it will be null so i have merged the comment_date with post_date so which post has the greater date (for comment_date or post_date) it will first and so on

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

For displaying the posts you have to first get the results by defining the WP's global variable for the database interaction i.e $wpdb

<?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"); 
?>

HTML

<?php foreach($results as $result){

<h1><?php echo $result->post_title;?></h1>
<div> <?php echo $result->post_content;?> </div>
// and so on the fields of wp_posts
<?php } // loop end ?>

Hope that is what you were looking for

这篇关于如何获得最新评论的帖子上面新提交的帖子在Wordpress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆