Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP Get WordPress Most Popular Posts By Total Comments

Get WordPress Most Popular Posts By Total Comments

This article shows How to retrieve and display the most popular posts by total comments on your WordPress template. A PHP function below will use the get_results function to run a SQL query to the WordPress posts table to get the top posts with highest comments.

1. Get WordPress Top Posts With Highest Comments

We can place the PHP function below in the sidebar.php file in the WordPress template folder or somewhere else such as footer.php or functions.php file.

<?php
function popularPosts($num) 
{
	global $wpdb;
 
	$posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
 
	foreach ($posts as $post) 
	{
		$id = $post->ID;
		$title = $post->post_title;
		$count = $post->comment_count;
 
		if ($count != 0) 
		{
			$popular .= '<li>';
			$popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';
			$popular .= '</li>';
		}
	}
	return $popular;
}
?>

<?php function popularPosts($num) { global $wpdb; $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num"); foreach ($posts as $post) { $id = $post->ID; $title = $post->post_title; $count = $post->comment_count; if ($count != 0) { $popular .= '<li>'; $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> '; $popular .= '</li>'; } } return $popular; } ?>

2. Usage

Simple call the popularPosts function and pass the number of returned posts.

<div id="tab_popularPosts">
	<ul>
		<?php echo popularPosts(10); ?>
	</ul>
</div>

<div id="tab_popularPosts"> <ul> <?php echo popularPosts(10); ?> </ul> </div>

Apr 24, 2012Hoan Huynh
JavaScript Detect Protocol (HTTP/ HTTPS) To Load Or Handle Accordingly5 GB Google Drive! Almost Time to Leave Dropbox?
You Might Also Like:
  • WordPress Get Recent Posts And Recent Comments
  • How To Track Website With Multiple Google Analytisc Accounts
  • Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated
  • ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares
  • PHP Get Facebook URL Total Comments, Total Likes, Total Shares
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Get Facebook Total Comments, Total Likes, Total Shares Of A Given URL
  • Delete WordPress Revision Posts, Its Relationships And Meta Data
  • Disable Pingbacks and Trackbacks In WordPress
  • How To Change Default Home Page Of WordPress
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

8 years ago PHPcomment_count, get_permalink, get_results, setup_postdata, Wordpress108
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,195 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,069 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,846 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,323 views
C# Read Json From URL And Parse/Deserialize Json
9,815 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development