2014-01-13 161 views
0

我尝试了很多东西,但不可能得到它的作品。订购WordPress的帖子最新评论

我终于删除了我的尝试

有一个插件呢?

SOLUTION:

在你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

这是一个黑客,但我不得不为我的项目做同样的事情。我结束了使用preprocess_comment过滤器每一个新的评论是在一个岗位上做出了一次更新后的“post_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); 
+0

似乎是一个不错的主意,但它不工作...我说这function.php – user1708580

+0

它的工作(见文章编辑) – user1708580

+0

这是伟大的,如果你看评论的帖子的一部分。感谢和+1 – henrywright

0

有一个plugin,但它一直没有更新在2年以上。它可能不再被维护或支持,并可能在与更新版本的WordPress一起使用时遇到兼容性问题。