Display the (x) most recent weblog entries from a specific user

PLEASE NOTE! The following snippets are user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

<?php
/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs of a certain user.
* If you want to increase/reduce
* the number of titles displayed..simply change the $listlength value
*
* for a different user change the $userid value
*
* works with drupal 4.6.
*
*/
$listlength="10";
$userid="8";

$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.uid = $userid AND n.status = 1 ORDER BY n.created DESC"), 0, $listlength));
print
$output;
?>

Could this be modified

stevryn - October 14, 2005 - 19:48

To show it for the "current user", and how would I do that?

Yes, fairly simply

merlinofchaos - October 14, 2005 - 21:21

<?php
/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs of a certain user.
* If you want to increase/reduce
* the number of titles displayed..simply change the $listlength value
*
* for a different user change the $userid value
*
* works with drupal 4.6.
*
*/
global $user;
$listlength="10";
$userid=$user->uid;
if (!
$userid) {
 
$userid = 8; // default in case not logged in. alternately could error out using drupal_access_denied(); return;
}
$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.uid = $userid AND n.status = 1 ORDER BY n.created DESC"), 0, $listlength));
print
$output;
?>

Show recent posts for user, broken down by month

gushie - February 17, 2007 - 15:38

I wrote the following block snippet to show the 50 recent blog entries for a user, with "February 2007" style subheadings for each month.
It is designed for a users blog index page, or on a blog posting.

<?php
if(!is_numeric(arg(1)))
    return;  
if(
arg(0)=='node'){
  
$node = node_load(arg(1));
  
$uid = $node->uid;
} elseif (
arg(0)=='blog') {
  
$uid = arg(1);
} else {
   return;
}
$result = db_query("SELECT created, title, nid, status, type FROM {node} WHERE uid = $uid AND status = 1 and type = 'blog' ORDER BY created DESC LIMIT 50");
   
$output = '<ul>';
    while (
$obj = db_fetch_object($result)) {
     
$thismonth = format_date($obj->created, 'custom', 'F Y');
      if(
$thismonth!=$lastmonth){
         if(
$output!='')
           
$output.='</ul>';
        
$output .= '<strong>'.$thismonth.'</strong><ul>';
        
$lastmonth=$thismonth;
      }
     
$output .= '<li>'.l(($obj->title), "node/".$obj->nid).'</li>';
    }
   
$output .= '</ul>';
    return
$output;
?>

Then to make it only appear on a users blog index/blog entry, I used the following PHP snippet in the "Show block on specific pages -> Show if the following PHP code returns TRUE" text area.

<?php
if (!is_numeric(arg(1))) {
   return
FALSE;
}
if (
arg(0) == 'blog')
   return
TRUE;
if (
arg(0) == 'node') {
  
$node = node_load(arg(1));
   if(
$node->type == 'blog')
       return
TRUE;
}
return
FALSE;
?>

 
 

Drupal is a registered trademark of Dries Buytaert.