[Drupal] How to get related Posts of the author by Drupal views

| | 2 min read

As Drupal sites are becoming increasingly flexible, it is obvious to list the nodes related to logged in user. This can be done with the help of PHP and MYSQL codes. However, we need not waste time if have the views module - the perfect icing on the cake !!! So, let's narrow down here.

Step 1 : Download Views module (https://Drupal.org/project/views).

Step 2 : Copy Views module folder to modules directory (sites/all/modules).

Step 3 : Enable the Module in: Admin >> Modules

  • Views

Step 4 : Create a new view namely "related_posts_by_author" and select 'Node' as View type.

Step 5 : Add the fields for the newly created view. Select 'Node' from the drop down and scroll down to Node : Title, check the add click on the button 'Add'.

Step 6 :Add Filters to the view, select Node from the drop down of Groups and check the box Node : Published, click on the 'Add' button.

Step 7 : Get the user id in order to list the nodes of that particular user. Let's do it with the Argument.

  • Start this by adding 'User : id' Argument.
  • In Configure Argument User: Uid, select 'Provide default argument' under 'Action to take if argument is not present:'.
  • Select PHP code from 'Default argument type:' and paste the below code :
  
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    $uid = $node->uid;
  }
  return $uid;

in the above code, check whether the page is a node page and then load the current node to get the user id. This user id is inturn passed as argument.

  • Click Update to save the 'Argument'.

Step 8 : Add a Block display, to get the view as block. Save the view, go to 'admin/build/blocks' and last but not the least, assign the block to your page.

Try this out, and get the related posts of the author.