WordPress Implements Articles Comment Ranking

Time:2019-9-16

An advanced use of the WordPress function Query_post() is to get a certain number of logs with the most reviews this week, that month, or the last 30 days.

The method of use is to place the following sections of code in the appropriate location in the theme template file that needs to display the hottest log, such as sidebar. php.

Comment on the maximum log at all times

Copy code

The code is as follows:

<ul> <?php query_posts(‘post_type=post&posts_per_page=10&orderby=comment_count&order=DESC’); while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php printf(esc_attr(‘Permalink to %s’), the_title_attribute(‘echo=0’)); ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

By default, this code displays the logs with the largest number of comments in the first 10 posts, and the number 10 can be changed to other values.
This week’s commentary is the most in the journal.
To display the most comment logs this week, we can use the following code, which adds some additional parameters to the previous code.

Copy code

The code is as follows:

<ul> <?php $week = date(‘W’); $year = date(‘Y’); query_posts(‘post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=’ . $year . ‘&w=’ . $week); while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php printf(esc_attr(‘Permalink to %s’), the_title_attribute(‘echo=0’)); ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

Comments on the most blogs in the last 30 days

Copy code

The code is as follows:

<ul> <?php function filter_where($where = ”) { //posts in the last 30 days $where .= ” AND post_date > ‘” . date(‘Y-m-d’, strtotime(‘-30 days’)) . “‘”; return $where; } add_filter(‘posts_where’, ‘filter_where’); query_posts(‘post_type=post&posts_per_page=10&orderby=comment_count&order=DESC’); while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php printf(esc_attr(‘Permalink to %s’), the_title_attribute(‘echo=0’)); ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

“30 days” can be changed to other values as needed (such as “1 year”, “7 days”).

Comments on most blogs this month
Similarly, to display the most commented logs of the month, you can use the following code:

Copy code

The code is as follows:

<ul> <?php $month = date(‘m’); $year = date(‘Y’); query_posts(‘post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=’ . $year . ‘&monthnum=’ . $month); while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php printf(esc_attr(‘Permalink to %s’), the_title_attribute(‘echo=0’)); ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

Additional notes are welcome.~