0

If someone put a comment to post, that post should be the first post of blog. ( Forums works like this).

I don't want to use forum plugin also. I need only, when a user submit a comment, that post goes to up as the first post of my blog.

Is it possible?

2
0

WordPress post queries don't natively support ordering by such specific comment information, only by comment count. Rest of comment-related things aren't stored in posts table and not available for easy querying.

While it's probably possible to cook up SQL for this, I think it would be more practical to store date of comment in its parent post's custom fields and order by that (which is natively supported), via modifying blog archive query in pre_get_posts hook or otherwise.

0

You can change the post_modified_date of the post on a new comment using this code

add_action( 'wp_insert_comment', 'update_post_modified_date', 10, 2 );
function update_post_modified_date(  $comment_id, $comment ) {
  wp_update_post( array( 'ID' => $comment->comment_post_ID ) );
}

Then while querying posts use modified as orderby parameter

1
  • You have a syntax error in your third line of code,missing a ) – Pieter Goosen Jul 14 '14 at 16:41
0

As already stated, this functionality is not natively supported by Wordpress, but that does not say it can't be done.

To accomplish this, you'll need to add a custom query_vars, and add custom sql queries and joining these queries through the posts_join posts_orderby filters. This code was stripped from the Filter By Comments plugin from Kevin Ingersoll. The complete plugin can be downloaded here. I've tested it thoroughly, and it works despite not being updated in more than two years.

Here is the custom code that makes it all happens

<?php
FilterByComments::getInstance();

class FilterByComments {

    static function getInstance() {
        static $instance;
        return $instance ? $instance : $instance = new self;
    }

    private function __clone() {}


    protected function __construct() {
        add_filter('query_vars', array(&$this, 'query_vars'));
        add_filter('posts_join', array(&$this, 'posts_join'));
        add_filter('posts_orderby', array(&$this, 'posts_orderby'));
    }


    function query_vars($vars) {
        $vars[] = 'orderby_last_comment';
        return $vars;
    }


    function posts_join($sql) {
        global $wpdb;
        if(get_query_var('orderby_last_comment')) {
            return $sql.'
                LEFT JOIN (SELECT `comment_post_ID` AS `post`, MAX(`comment_date`) AS `date` FROM `'.$wpdb->comments.'` WHERE `comment_approved`="1" GROUP BY `post`) AS `last_comment`
                    ON `last_comment`.`post` = `'.$wpdb->posts.'`.`ID`
            ';
        }
        return $sql;
    }

    function posts_orderby($sql) {
        if(get_query_var('orderby_last_comment')) {
            return '`last_comment`.`date` DESC'.($sql !== '' ? ', '.$sql : '');
        }
        return $sql;
    }
}
?>

It is simple to implement via pre_get_posts or WP_Query.You can work directly with the query_vars orderby_last_comment. To order posts by newest comment, just set orderby_last_comment to true or 1

USING pre_get_posts

function order_by_newest_comment( $query ) {
    if ( $query->is_main_query() && $query->is_home() ) {
        $query->set( 'orderby_last_comment', true );
    }
}
add_action( 'pre_get_posts', 'order_by_newest_comment' );

USING WP_Query

$the_query = new WP_Query('orderby_last_comment=true');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.