We recently discussed about How to: Show Similar articles in WordPress (without plugin).
It is obvious that every blogger likes to get Feedback or comments for his article. In case you want to know about the most commented articles, most discussion happened or display favorite items in your WordPress blog then there is no need of any additional plugin.
I’ll help you to do this by using relatively easy code that will help you to find out the article which is most commented without plugin in your blog.
The code to determine the most commented articles
The following code as always you can complete in your functions.php add your themes:
At first glance, the code may look a bit long and complex, but don’t be frighten. In the first part of the function (line 5-17) aims to influence the determination of the most commented articles. Here you can even set whether your comments that you have written yourself (admin Comments) are to be calculated with or not. Furthermore, it can be set individually to determine the most commented article applies only to outgoing comments in the last x days.
The second part (lines 19-38) is then used to determine the most commented articles in the database.
Subsequently, in the third part - the output generated (line 40, 47). Ensure the items are packed in an ordered list, and each item is displayed as a list item, each item is of course linked accordingly.
SELECT (line 20-23)
Selects are the IDs, title and number of the article comments.
FROM (line 24 - 26)
We refer to the posts table and if $ dayInterval is greater -1 then it takes a INNER JOIN with the comments table.
WHERE (line 27 - 32)
The terms include the following:
- is a product and not a side
- the article has at least a Comment
- the article is not in the Trash
- the comment is not from the admin user with id 1 (this condition applies only if $ count admin comments equal true is)
- The comment has been posted within the last x days (this condition applies only if $ dayInterval is greater -1)
GROUP BY, ORDER BY and LIMIT (lines 33-37)
Is grouped by the article ID and is ordered descending by number of comments. Via the parameter $ limit the number of records to be selected may be specified.
The code to display the most commented articles
The following code can be placed anywhere in your WordPress theme. So if you want eg the list of the top 5 most commented and thus most appreciated products of your blogs in the sidebar, you can add the following code example in the sidebar.php insert:
- echo get_popular_posts_by_comments ();
To display the most commented articles in the past 30 days with no consideration of their own, so the comments written by the admin, the function would be called like this:
- echo get_popular_posts_by_comments (30, false);
The presented code snippet allows you to easily display the most commented article of your blog.
A full-time blogger & Digital Marketing Expert in India. An early adopter who loves to try out various Digital, Blogging & Social media tools along with exploring some cool iOS and Android apps.