In this post, I’m going to show you how you can easily leverage the power of Views 2 to create a simple block that displays “Related Posts by this Author” information. To break it down, here’s exactly what we need to do: when a user has navigated to a specific node, we want to use that node author’s user ID as a key to load other posts by that author.
Sure, you could use a PHP block to accomplish this, but that would require some pretty serious knowledge of PHP/MySQL, not to mention how to securely make database calls within Drupal. There are loads of PHP snippet posts on Drupal.org for this kind of thing, but why waste time hand-coding, when Views 2 makes this all a piece of cake? And seriously, it is a piece of cake once you get a general idea of how it works.
Technical Requirements
To get started, here’s what you’ll need:
- Drupal 6 (I’m using 6.10 in this tutorial)
- Views 2 (I have 6.x-2.3)
- Multiple users with associated nodes (I used the Devel module to generate users and nodes for this demonstration)
Alright, let’s do this!
Once you’re ready to get started, head over to the Views home page at /admin/build/views.
1. Create a New View
Create a new view by clicking the “Add” tab at the top of the page

2. View Settings
Name this new View: ”related_posts_by_author”. For the description, call it something like “Related Posts by Author”. The view tag field is optional – use it as needed. The most important part of this first screen is to select ”Node” as the View type.

3. Add Fields to the View
Now let’s set up our defaults for this view. I like to start by adding a “Field” so that Views doesn’t yell at me. Choose “Node” from the dropdown and then scroll to Node Title, check that box and click “Add”. On the screen that follows, remove the default label text and select “Link this field to its Node”. Click Update.

4. Add Filters to the View
Now that we have some output, let’s start filtering this down. Click on “Items to display”, adjust to a number that suits your needs. I went with 5. Next, let’s add a filter that will return only “Published” nodes. Click the plus under “Filters”. Choose “Node” from the dropdown, then select “Node: Published” and click “Add”. Choose “Yes” on the corresponding screen and click “Update”. Add any other filters you need here as well, such as “Node: Type”, or “Node: Promoted to front page” – you get the idea.

5. Sort Parameters
I added a “Sort Criteria” to display nodes sorted by their “Post Date”.

6. Arguments
Now that we have some decently filtered output and our Views Defaults set up, let’s get to the heart of this tutorial. How do I tell Drupal to only load posts by the current author? This is where “Arguments” come into play. Basically, we need to tell Views to only include nodes that have the same user ID as the user who created the current node. “Arguments” will allow you to pass that value into your query and match it against the values you are pulling.
- Start by adding a “User:Uid” Argument.
- On the following screen, leave the “Title”, “Wildcard” and “Wildcard Title” fields at their defaults.
- Choose “Provide Default argument” from the “Action to take if argument is not present: ” options. This will allow you to load in your own values.
- Next, choose “PHP Code” from the options under “Default argument type: ”. This will let us use our own PHP to grab the User ID from the current node.
- Now for the magic: Enter the following code into the “PHP argument code: ” textarea.
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$uid = $node->uid;
}
return $uid;
- Basically, this code first checks the current URL to make sure it matches as a node page. It then loads the current node with the “node_load();” function using the node id from the URL. Then it sets a variable with the User ID as its value ($uid = $node->uid;). Finally, it returns this value to the view. Leave all other settings at their defaults. Click “Update” to save this argument.



- Note: By default, no User ID is passed to the view, so your preview will show “No query was run.” To check and see if your argument is working enter a User Id into the “Arguments: ” field and click “Preview”. You should then see posts from that user only.

7. Add a Block Display
Now that we know our view is accepting the dynamic parameter, all that’s left is to expose this view as a block. Simply choose “Block” from the left most drop down and click “Add Display”. This will use all of the defaults that we’ve already set up.

Next, give this block a name by clicking the link under “Block Settings” and modifying the form that follows.

8. Last But Not Least…
Finally, Save your view. Add your block to the pages you need at “admin/build/blocks” and you’ll be in business.
That’s it – you now have a dynamic “Related Posts by this Author” block, created with Views! Nice job!

Got any questions?
If you have any question about this Drupal tutorial, please leave a comment below and we’ll help you out!






Hey, tried this, however nothing comes up when i put the block in the area.
lbrahim – It would be somewhat difficult for us to help you debug your issue without some details. Can you please go back and make sure that you followed all of the steps first, and then if things are still not working, let us know.
Did you make sure to create some dummy data to output in the block? Many times, people forget this step.
hey actually it just started working for some weird reason. I was wondering how do you go about customising the area. For instance, putting a heading for the area or adding css/changing font color, displaying an image next to the title of the node, etc..
All of that can be accomplished using the same view you created in our tutorial. Each view is assigned a specific id that can be targeted using CSS. Just take a look at the source code on the page that the view is being output and you should be able to find the information that you need.
Thanks alot, really helpful.
Just a couple more things.
I was wondering whether i could put a little snippet of one of the stories in the view.
For example:
Posted By: ‘username’
On: 23rd March 2009
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus risus. Class aptent taciti
Furthermore, is there a way i can change the header of the view. i.e to put a line right under it. However right now the line appears after all the content is displayed.
Great tutorial! Arguments are a feature that I didn’t realize I needed until I found this piece. (Still working out the difference between filters, arguments, and view types… I mean, it’s all just SQL at some level, right? Oh well.)
I came here looking to learn how to do a similar thing: I needed to match on the node ID rather than the user ID. I figured I could just adapt the PHP sample you gave, so I did, and it worked … and then I saw that one of the default argument types was to match on the node from the URL. *Durf.*
Now I know what’s the what, though. Thank you!
Great tutorial (if only there was a final snapshot of your results
)
I have a question though. Using Devel is not required to generate this view (suppose we already have users that post content) right?
m.
@Ibrahim – If you would like to add additional content to your view, you can do so under the Fields section. For example, if you wanted to add the ‘teaser’ of the nodes, you could select “Node: Teaser”. As for changing the look of the view, there are a few different ways to approach this. You could simply use css to style the markup that Views spits out, or you could create views templates, (that’s a tutorial for another day).
@Cameron Goble – Thanks! Glad you found the tips useful. Arguments are a bit confusing – even for PHP/mySQL pros.
@Myrto Kamenopoulou – Thanks! I’ll add a snapshot of the results – good call there. As for your question: No, you don’t need to use Devel to create the users/content. I only used Devel to quickly generate some nodes and associated users. It will work with existing users and content.
Hey,
Nicely written tutorial. However, doesn’t the PHP code you include do the same thing as if you selected “User ID from URL” as the default argument type? I’m not sure I’m clear on what the difference is.
@andyjeanee – Thanks for your comment! In this case, the user id will not be present in the URL because we will be viewing a node. So the the node ID is really the only data available in the URL. That’s why we use node_load function to grab the user ID of the author of that post. If we we viewing a user profile, than the User Id would be available via the URL. Hope this helps!
Just spent about a half hour trying to figure out why this wouldn’t work for me, and I found what appears to be a mistake…
In the PHP code, “$node->id” should be “$node->uid”. The screenshot above has it right, but the code snippet does not. Chris, could you fix this typo so others don’t make the same mistake?
- Mike
Thanks for catching that, Mike. We’ve updated the code on this page.
Also, per the above instructions, the CURRENT page might appear in the list of OTHER pages but the current author. I can’t seem to figure out a way to EXCLUDE a certain page from the block using the Views 2 interface. Any further explanation or ideas would be appreciated.
OK… one more final comment. I just answered my own question (and more), and I think my “findings” might be quite useful here.
First off, andyjeanne seems to be correct; all of this can be done without a line of PHP code. I’m not sure exactly why, but doing everything you did above except selecting “User ID from URL” instead of “PHP Code” works for me. It’s just ugly to use PHP when you don’t need to. And as someone who is obsessed with efficiency, doing it this way almost surely avoids costly processing of redundant DB calls (information which has already been loaded by PHP).
Second, in order to exclude the current article, do a similar exercise with “Node:Nid” instead of “User:Uid,” select the “Node ID from URL” radio instead of “PHP Code,” and check “Exclude the argument” at the bottom.
Hope this information helps!
- Mike Albrecht
p.s. for all it’s worth, Views is one of the most frustrating, arbitrarily esoteric system I’ve ever used.
Thank you for tip how to exclude the current node. I was searching for it, but somehow didn’t see “Exclude the argument”. From now I know how to do
Thanks.
THANK YOU! THANK YOU! THANK YOU! I have been searching for lord knows how long to get a block to display a field only from the node it came from. This worked perfectly! Just had to change uid to nid. THANK YOU!
Hey guys. First off I would like to say great tutorial. Now, I’m wondering if you guys could show a way to create a “About Author” block using Views. It should be simple using the Profile module as you can display the fields using Views.
Thanks for the request, Entr3p. We’ll see what Chris can come up with! I’ll be sure and shoot you an e-mail if he decides to whip together a quick tutorial!
Thank you very much.
I’m using it to match CCK fields and it’s great. The big issue was access to CCK content with that awful array ($node->field_myfield[0]['value'] etc.)
Thank you again from Spain.
Great Tutorial.
How do I apply this if it’s for user profile fields using core profile? Not a node, right? (sorry, I still learning). I want to create a block of the contact information (core profile fields) in the user’s profile page. I have followed the steps and it doesn’t seem to be working for me – I wondering if it’s because it’s the core profile. Thanks
Katie,
Actually, Drupal provides this functionality by default. If you visit your block administration page you should see a block titled, “Author Information” – this will display user information gathered with the core Profile module. I believe it will even attach the user photo if you are using that core feature as well.
Hope this helps and happy Drupaling!
Hey this worked, been searching for 2 hours for this solution. I still dont understand it, but I will have to look over it a few times. Thanks again
Nice article!
Question: If you were doing a similar thing but using a node-referenced nid instead of the user id, what code would you use for the argument? For example if you have a profile content type and a story content type (each story with a cck field with node reference to a profile node), and wanted the teaser of each node-reffed profile to appear in a block on the same page of the story..
(I’d thought thought by setting up a relationship and then using the argument I could accomplish this, but so far no luck.)
I loved this article, it was helpful — but say I want to pull this block on user pages (www.drupal-site.com/user/userfirstname-userlastname) and don’t have an actual “ID” number to work with? How can I pull content that user has created on their profile pages, basically?
Andrew, actually, you do have a user id – when you’re on a user’s page, the user ID is in the URL. You can just use that instead of loading a node from the database. Select “User ID from URL” as your default argument and you’ll be all set!
This article is very enlightening, thank you for that. I have identical problem as Andrew and I think the answer only fixes problem when a user page URL contains a numeric uid. If someone has chosen to use autopath module to translate these urls to more convenient eg. page.com/profile/john, the view default argument won’t work (I’ve tried all combinations). And the question is how to get uid number from user-raw value and set default parameter using php code like in article or possibly how to force view to parse user-raw text parameter instead of uid number. Is this actually possible ?
Bravo!
I have been banging my head against a wall (for weeks) trying to sort out a solution for a similar problem!
1,000,000 kudos points!
Respect!
thank you for this understandable tutorial, worked fine for me and saved from all those unneccesary modelus. great
Great tutorial, very useful, thanks!
Thanks for this! So simple. Brilliant tutorial!
One question though. Would it be possible to have a title of the block that would be dynamic as well. Let me explain. Instead of “Recent posts”, you would have “Posts by Don”. So the header would reflect the author’s name (the creator of the node). Is it possible to do that with views?
GREAT! thank you!
you are a prince. thank you for this!
O, and I second @Jan’s question about dynamic titles.
I figured out how to do dynamic titles. It’s real simple as well. Just in the Title line of the Argument add this
Recent posts by %1
the %1 will be replaced by the argument variable.
Thanks Jan! case closed.
Thanks sir,
for your great tutorial. I am try to do this some days but I couldn’t. Now I do it successfully.
By using your tips, I am also try to do related comments by this author. It’s working fine, but problem is when I select “(Node) Comment: Body from fields” there have no option to link to comment.
But when I select “Comment: Title Title” where have option “Link this field to its comment” Please tell me how I can “Link this field to its comment” in (Node) Comment: Body.
Another thing If anyone can help me, how can I show posted comments in this blog by all user in drupal. Give me some idea, I will try to do it myself.
Note: I’m not a programmer. I new in Drupal & try to buildup multi blog in drupal.
Thanks
Hi,
I have had no problem with following this tutorial and getting it running in an older version of Drupal, I have upgraded to the latest release on another server and have run in to problems.
Now I find when I run the querie – the following code is returned (with no results). I was using node 822 to view related posts from user/13
SELECT node.nid AS nid, node.title AS node_title FROM node node INNER JOIN users users ON node.uid = users.uid WHERE (node.status 0) AND (users.uid = 822)
It looks to me that (users.uid = 822) should be returning as user (users.uid = 13)
Has anyone had a similar problem? Or am I missing something fundemental?
thanks
Luke
Thanks for this. Simply a great tutorial — easy to follow, well written and actually works. And along the way, I learned something new about how to use Views. Arguments always confused me, now I see how valuable they can be.
Very helpful tutorial, I’m glad that I found it after couple days of head-desk banging, as this is exactly what I was looking for!
As I am using Drupal 7 I was initially slightly confused, as Arguments were renamed to Contextual Filters.
Everything seems to work, but I am encountering notice message on any screen where the given view is excluded, not sure why… (I tested this method on clean drupal installation).
The message is:
Undefined variable: uid in eval() (line 5 of [...]\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(49) : eval()’d code).
Any Ideas?
Sorted now, it turned out that in D7 it is possible achieve the same result without writing PHP arguments, I just have to choose User ID from URL as a default value and check “Also look for a node and use the node author” option.
Thank you for the tut, helped me a lot!
Glad you got it, Lucas! Thanks for the info on the D7 workaround.
hey,
came across your post as I am trying to accomplish a similar task in drupal 7 – I tried applying the drupal 6 arguments php, it works in views pane when I put in the uid in filter but not on the block in the site.
I am trying to list the articles written by a user on the user’s page, I couldn’t find the two options you referred to – (uid from url)
Do you think you can add a bit more detail, i would really appreciate it, maybe i am missing something simple
Hi,
All I did was just creating a contextual filter User:Uid instead of adding PHP arguments. Then I selected “Provide default value” radio button, and selected “User ID from URL” as type of the value and checked option “Also look for a node and use the node author”.
Here is the screenshot of the filter config:
http://i.imgur.com/x8HTW.jpg
I hope it is relevant to your problem
Thanks for that tidbit! Works awesome!
This article helped me sooooo much! Especially since I’m new to Drupal. Thank you!
Note to the users above and to anyone interested in applying this tutorial to views in drupal 7.
1. you must require the following relationship first: content:author.
2. only then does the contextual filter of user:uid become available.
3. then afterwards select the radio for: provide default argument, the select list for user id from url and the checkbox for, Also look for a node and use the node author.
4. Apply and save, and you should find your view works all nice and dandy now.
In d7 – the above comments didn’t work for me… im using jcarousol too.
Thank you for the tutorial!
If you don’t want to show the actual page in the block too and you set the block to show x entries, then you can also just set the offset to 1.