Community

Related content block by taxonomy terms using Views3 + exclude some terms

First of all, I did everything by this guide:

  1. Create a block view
  2. Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
  3. Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
  4. Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
  5. Turn on Views aggregation
  6. Assuming you are listing title only, edit the title field to use the Relationship you set up in #4 above.
  7. Add new sort criteria of Content: Nid. In aggregation settings, select Count. Use relationship from #4 and sort descending
  8. Add Contextual filter -> Content: Nid -> Use relationship from #4 -> Provide default value - Content ID from URL -> Scroll down and expand "More" then check "Exclude" to remove current node from the view

And it works like a charm. However I have no idea how to exclude some of the taxonomy terms (by id or name) here, because I use some of them in my all posts (kinda related to category) and now it makes a little mess in related content block.

I also tried Contextual filter > Has taxonomy term ID > Provide default value > PHP Code:

<?php
$node
= @node_load(arg(1));
if(@isset(
$node->field_tags)) {
    foreach(
$node->field_tags['und'] as $term) {
         if(
$term['tid'] != 6 && $term['tid'] != 8) { $terms[] = $term['tid']; }
    }
    return
implode('+', $terms);
} else {
    return;
}
?>

Unfortunately, then the related content block doesn't show anything.

Any ideas?

Comments

A way to exclude

Just create another vocabulary for the universal terms.

Oliver "Andrew" McCloud
http://queryblitz.com
Quick and easy feedback for your websites.