{"id":240,"date":"2009-03-22T23:57:20","date_gmt":"2009-03-22T18:27:20","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/?p=240"},"modified":"2009-03-22T23:57:20","modified_gmt":"2009-03-22T18:27:20","slug":"show-popular-posts-in-wordpress-without-a-plugin","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2009\/03\/show-popular-posts-in-wordpress-without-a-plugin\/","title":{"rendered":"Show Popular Posts in WordPress &#8211; without a plugin"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2009\/03\/halo_wordpress.jpg\" alt=\"WordPress Helo Effect\" title=\"WordPress Helo Effect\" width=\"300\" height=\"233\" class=\"alignnone size-full wp-image-238 intro\" align=\"right\" \/><\/p>\n<p class=\"intro\">A <strong class=\"highlight\">list of the popular posts<\/strong> of the blog is a standard feature in many blogs. There are quite a few plugins that offer this feature&#8230;<\/p>\n<ul>\n<li><a href=\"http:\/\/ajaydsouza.com\/wordpress\/plugins\/top-10\/\">Top 10 &#8211; A Page Counter and Popular Posts plugin for WordPress<\/a><\/li>\n<li><a href=\"http:\/\/alexking.org\/blog\/2005\/05\/23\/popularity-contest\">Popularity Contest Plugin (beta)<\/a><\/li>\n<li><a href=\"http:\/\/wordpress.org\/extend\/plugins\/wordpress-popular-posts\/\">WordPress Popular Posts<\/a><\/li>\n<li><a href=\"http:\/\/boakes.org\/most-wanted\/\">MostWanted &#8211; a Popular Posts Plugin for WordPress<\/a><\/li>\n<\/ul>\n<p>Now, lets see how to do it without using a plugin. In case you are new to this blog, I am currently working on the <a href=\"http:\/\/www.bin-co.com\/blog\/2009\/03\/wordpress-plugin-killer-series\/\">Plugin Killer Series<\/a> &#8211; a series of post in which I explain how to duplicate the functionality provided by some wordpress plugins &#8211; without having to install the plugin.<\/p>\n<p>These <strong class=\"highlight\">plugins work by adding a view counter for each post<\/strong> &#8211; whenever a user visits a page, it will increment the count for that page by one. Unfortunately, WordPress does not provide this feature. But there is <strong class=\"highlight\">another indicator for the popularity of a post &#8211; its comment count<\/strong>.<\/p>\n<h2>The Code<\/h2>\n<h3>Getting Comment Count &#8211; SQL<\/h3>\n<p>So the first step is to get the list of post with the most comments. To get that, we can use the following SQL statement&#8230;<\/p>\n<pre><code class=\"sql\">SELECT id,post_title FROM wp_posts ORDER BY comment_count DESC LIMIT 0,10<\/code><\/pre>\n<h3>Listing The Data &#8211; PHP\/SQL<\/h3>\n<p>This query will return just 10 posts. If you want more(or less) post, just change the number after LIMIT accordingly. The PHP code for executing the query and getting its result is&#8230;<\/p>\n<pre><code class=\"php\">$popular_posts = $wpdb-&gt;get_results(\"SELECT id,post_title FROM {$wpdb-&gt;prefix}posts ORDER BY comment_count DESC LIMIT 0,10\");\nforeach($popular_posts as $post) {\n\t\/\/ Do something with the $post variable\n}\n<\/code><\/pre>\n<h3>Final Output as Links in a List &#8211; HTML\/PHP\/SQL<\/h3>\n<p>Next step is to get the URL of each post. The recommended way of doing this is by using the &#8216;<a href=\"http:\/\/codex.wordpress.org\/Template_Tags\/get_permalink\">get_permalink<\/a>&#8216; function. Another thing we have to do is to map the result as an HTML list&#8230;<\/p>\n<pre><code class=\"php\">&lt;li&gt;&lt;h3&gt;Popular Posts&lt;\/h3&gt;\n&lt;ul class=\"bullets\"&gt;\n&lt;?php \n$popular_posts = $wpdb-&gt;get_results(\"SELECT id,post_title FROM {$wpdb-&gt;prefix}posts ORDER BY comment_count DESC LIMIT 0,10\");\nforeach($popular_posts as $post) {\n\tprint \"&lt;li&gt;&lt;a href='\". get_permalink($post-&gt;id) .\"'&gt;\".$post-&gt;post_title.\"&lt;\/a&gt;&lt;\/li&gt;\\n\";\n}\n?&gt;\n&lt;\/ul&gt;\n&lt;\/li&gt;<\/code><\/pre>\n<p>Note: The HTML part of the code may need to be changed &#8211; depending on your theme.<\/p>\n<p>Add this code to the <code>sidebar.php<\/code> file in your theme &#8211; and you are done!<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A list of the popular posts of the blog is a standard feature in many blogs. There are quite a few plugins that offer this feature. In case you are new to this blog, I am currently working on the <a href=\"http:\/\/www.bin-co.com\/blog\/2009\/03\/wordpress-plugin-killer-series\/\">Plugin Killer Series<\/a> &#8211; a series of post in which I explain how to duplicate the functionality provided by some wordpress plugins &#8211; without having to install the plugin.<\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,21,25,28,31],"tags":[212,213,218,251,283,304,307],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-blogging","category-php","category-scripts","category-tutorial","category-wordpress","tag-plugin","tag-plugin-killer","tag-popular","tag-series","tag-top","tag-wordpress","tag-wp"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=240"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/240\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}