按最新评论排序Wordpress帖子

按最新评论排序Wordpress帖子,wordpress,comments,Wordpress,Comments,我尝试了很多事情,但都不可能成功 我终于删除了我的尝试 有插件吗 解决方案: 在function.php中: /** * Comment_pre_save filter updates post_modified date * to coincide with new comments. Since posts are ordered by * post_modified, this will 'bubble' up more active posts to the top */

我尝试了很多事情,但都不可能成功

我终于删除了我的尝试

有插件吗

解决方案:

在function.php中:

/** 
 * Comment_pre_save filter updates post_modified date
 * to coincide with new comments. Since posts are ordered by 
 * post_modified, this will 'bubble' up more active posts to the top
 */ 
add_filter('preprocess_comment', 'update_post_modified');

function update_post_modified($comment_data){
    global $wpdb;
    $wpdb->update( 
        $wpdb->posts, 
        array( 
            'post_modified' => current_time('mysql'),   // string
        ),
        array( 'ID' => $comment_data['comment_post_ID'] ), 
        array( 
            '%s'
        ), 
        array( '%d' ) 
    );
    return $comment_data;
}
在index.php中:

<?php $posts=query_posts($query_string . '&orderby=modified'); ?>  

有一个,但已超过2年没有更新。它可能不再被维护或支持,并且在与较新版本的WordPress一起使用时可能存在兼容性问题。

这有点像黑客,但我不得不对我的一个项目做同样的事情。每次使用
preprocess\u comment
过滤器对帖子发表新评论时,我都会更新帖子的“post\u modified”字段:

/** 
 * Comment_pre_save filter updates post_modified date
 * to coincide with new comments. Since posts are ordered by 
 * post_modified, this will 'bubble' up more active posts to the top
 */ 
add_filter('preprocess_comment', 'update_post_modified');

function update_post_modified($comment_data){
    global $wpdb;
    $wpdb->update( 
        $wpdb->posts, 
        array( 
            'post_modified' => current_time('mysql'),   // string
        ),
        array( 'ID' => $comment_data['comment_post_ID'] ), 
        array( 
            '%s'
        ), 
        array( '%d' ) 
    );
    return $comment_data;
}
编辑:

忘了添加一个位,无论你在哪里有你的帖子循环,确保你在修改日期前订购,否则这个黑客将无法工作。例如:

global $wp_query;
$args = array_merge( $wp_query->query, array( 'orderby' => 'modified' ) );
query_posts($args);

这似乎是个好主意,但不起作用。。。我把这个添加到function.php如果你在帖子中看到评论,那就太好了。谢谢和+1