Add related articles via View

When you create content in your site, you really want to recommend additional content to your users, so that if you read this blog post, I'll be able to recommend another blog post I've written on a similar subject.
Related Articles:

Let's start with what I think is the most common scenario when creating a blog post. You create a blog post, tag it with all the relavent tags, and you want to show in the sidebar additional blog posts that have similar tags.

for this we'll start with a plain new installation of Drupal 7, which comes loaded by default with the "Article" content type, as well as the the "Tags" Vocabulary and field on Articles.

Next, download, install and activate the Views, and Views_UI (bundled with views) modules.

Now, let's create some dummy articles:

Title: Blog post about Drupal and Web Design, Tags: Drupal, Web
Title: Blog post about Responsive Design, Tags: Drupal, Responsive
Title: Blog post about Cats, Tags: Cats
Title Blog post about Responsive Drupal Cats, Tags: Drupal, Cats, Responsive

Next, create a new view:

Call it Related Articles
Show: Content
Type: Articles
Create a page: Unchecked
Create a blog: Check
Continue & Edit

Ok, the first thing we need to do is access information about out Tags taxonomy terms on the article. Since taxonomy terms are stored on the article as reference field between the article node entity and the taxonomy term entity, we need to add a relationship between the node and the taxonomy based on the Tags field.

Click on the "Advanced" link to show the...well advanced options.

Add a Relationship, and search for the taxonomy term reference field name - Tags. Check the "Content: Tags (field_tags)" option, and click the "Apply (all displays)" button.

Click "Apply (all displays) again.

Now we can do the smart stuff, basically this is what we want to happend: when you're viewing an article, you want to show other articles that have the same tags in common with the current article.

Currently our view will show all the articles (click "Update preview" at the bottom), so we want to filter out all the irrelavent articles.

Add a Contextual Filter, filter to "Taxonomy Term", and no check "Taxonomy term: Term ID", we want to filter the results according to Taxonomy term ids.


Views expect contextual filters to be given via the url, on a page with the url "blog", you'd pass a contextual filter like so: "blog/1". But we don't want to modify the url, we're just viewing an article right?

Under "When the filter value is NOT Available" select "Provide default value"

Next, under "Type" select "Taxonomy term ID from URL" (we're on an article, and we want to get their terms/tags from that article).

Now, here comes the real magic: Check " Load default filter from node page, that's good for related taxonomy blocks" , and "Limit Items by vocabulary", and "Tags" (this is optional, if you have multiple taxonomies on an article you don't have to limit the results).

Multiple-Value handling -> "Filter to items that share any term" - do we want only articles that share all the tags, on is just one enough.

Click on the "More" link at the bottom.

Check "Allow multiple values", I expect you'll want to use more than one tag on an Article, I know I do.

Let's save the view.

This view created a block for us called "Related Articles", lets place it in the sidebar.

Next, let's visit the first Article we created "Blog post about drupal and web design"

Ok, looks good but we have the same article multiple times...

Edit your view, and under "Other" -> "Query Settings" check the "Distinct" option.

Save your view and visit one of the articles we created.

Look! we have three articles in the sidebar... lets go to article number 3 (the cats one).

Here we have only two articles, the current one, and responsive drupal cats -> SUCCESS!

well, almost...why do we need the article we're looking at in the related articles block, of course it's related it's the same article.

Let's edit the view again.

So we want to filter out from the view the article we're looking at. Let's add another "Contextual Filter". This time, we want to filter out a particular article (node) so lets add "Content: Nid".

In the settings view, choose "Provide default value" again, but this time select "Content ID from URL" since want to grab the ID of the current article.

Click on the "more" link again, and check "Exclude"

We're excluding the id of the node that we're viewing.

Save, and preview.