WordPress hack: Show your top contributors without a plugin

March 24, 2010 at 12:15 pm

Do you ever wished to be able to list your top contributors without using a plugin? If yes, you’ll be happy with this recipe that will show you how to do it.

Simply paste this code where you want your top contributors to be displayed. Note that this code use the mysql_* functions instead of the $wpdb object. This is not the better way to do this, but it do the trick ;)

<?php
include($_SERVER['DOCUMENT_ROOT']."/wp-config.php");
mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());

$sql = "SELECT
".$table_prefix.users.".".user_login.",count(*)\n"
    . "FROM
".$table_prefix.posts.",".$table_prefix.users."\n"
    . "WHERE ".$table_prefix.posts.".".post_parent."=0
and
".$table_prefix.posts.".".post_author."=".$table_prefix.users.".".ID."\n"
    . "Group by
".$table_prefix.users.".".user_login."\n"
    . "Order by count(*) DESC\n"
    . "Limit 0,10"; 
$result = mysql_query($sql) or die(mysql_error());
echo "\n";
echo "<ul>";
while($row = mysql_fetch_array($result))
{
    echo "<li><strong>";
    echo $row['user_login'];
    echo "</strong>&nbsp";
    echo "(";
    echo $row['count(*)'];
    echo ")</li>";

}
echo "</ul>";

?>

This recipe has been submitted by Sidou. Do not hesitate to send me your recipes if you want them to appear on my site!

Tags: