Hi--I'm using Views to create a list of books, but I'm running into problems with the sort criteria. I want to sort by author, then series name, then series number, then title. For the title I'm using the node title, and that sorts just fine. But for the other information I'm using the taxonomy module: I have a controlled vocabulary for the series number information, and free vocabularies for the author and series name information. I also have them weighted so that author is lightest, then series name, then series number. However, telling Views to sort by taxonomy terms seems to do nothing. What's going wrong?

Comments

roisin47’s picture

Bumping this up since it's yet to get an answer.

pshadow’s picture

I can't get this to work either. I have set the Taxonomy vocab to lowest weight and selected Taxonomy: Term Name and Asc in Sort Criteria. However this doesn't seem to work at all. The page displays them randomly. I'm trying to get it to sort the page nodes by Vocab 1, Alphabetically for the terms in vocab 1, Asc. Any ideas?

Blueeeeie’s picture

Hi,

If you're looking to sort the terms in your vocab by alphabetical order, you can check out the tutorial here.

Hope this helps

---
www.drupaldiy.com - Showcase your Drupal site

nathan573’s picture

Sorting by taxonomy terms in a View isn't a stock function, at least when dealing with CCK content types.

Here's how to do it...

Make sure you have the right modules enabled:

Views: http://drupal.org/project/views
CCK Taxonomy: http://drupal.org/project/cck_taxonomy
Taxonomy Fields: http://drupal.org/project/taxonomy_fields

Create a vocabulary and assign it to the content type your View is displaying

Create terms in the vocabulary that you want to sort by, setting the weight to -10 for the highest ranking term, -9 for the next down and so on. You might want to add a "-None-" category.

Configure your View filter to the content type for the first filter, "Taxonomy: Terms for [your vocabulary name]" as the second. Set the Operator to "Is One Of" and highlight all the terms in the select box.

Add the sort criteria "Taxonomy: Term Name", Ascending

That's it. You are limited in the number of weight slots you have, but it's still useful.

Striky2’s picture

Hi,

Ok... I get the answer, very astute! But let's assume now you don't know the books list and you want it to be dynamically sorted however?

To be more precise, I have the same issue, all my books are CCK objects but they have a title which is a unique taxonomy term. I'm trying to figure out how my view could be possibly allowing people to do a basic alphabetical (ASC / DESC) search among these terms?

I'm sure that I should modify the taxonomy_views.inc (located in views/modules) in order to do that. But it seems really hard to do that!

If anyone can help me on that point, I would really appreciate this.

Thx!

ar-jan’s picture

I tried this method in D6, with Views2. I used just Views and CCK but it seems that this has all the options to create the content type etc. I followed your steps (and tried a lot more), but I cannot get the sort order I want. It's the same term that gets listed first over and over again. Do you know if this can still be done, and how?

tonydearaujo’s picture

Hi Nathan, thanks for your description.
I was having a problem where some of my terms were sorting at random.
For me the solution was definitely the order of filters exposed. As you wrote, we need to list the sorting criteria on the CONTENT TYPE first, and the sorting criteria on the VOCABULARY TERM second. I had it the other way around and that's why my terms sorting was erratic.
Thanks!
tony

dmnd’s picture

does this help?
Requirements:

* Views: http://drupal.org/project/views
* CCK Taxonomy: http://drupal.org/project/cck_taxonomy
* Taxonomy Fields: http://drupal.org/project/taxonomy_fields

Now ORDER is crucial in views. The first filter should be the type of content; choose the type where you applied the terms to. It can be page type or any content type you created. Secondly, the next filter must be (the numbered) terms you associated with this content type. Add any other filters if required, but only after numbered term filter (named Order).

The next and final step is to add a sorting criteria. Since views does not allow specific 'sort by term', just add Taxonomy: Term Name in the Sorting Criteria (as seen below).

The sorting criteria sorts the first filtered term, then the second, followed by the third, and so on. Keeping that in mind, you can expand on this and create a custom hierarchal list of terms, oh my!

dmnd’s picture

I do not know if this is the source of your issue but this confused me for a long time.

In views, when you pick sort by taxonomy, it sorts by the first taxonomy of that node, whether or not if shows up in the view.

ar-jan’s picture

Hey, thanks for your feedback. I was actually just about to give this issue another try, so I will use your suggestions.

What you were saying about the first taxonomy of the node, I did assume so, but just to confirm: this depends on the general order of the vocabs (by their weight) also, right?

Maybe what i'm doing is still problematic, though. I'm using D6, btw.

Let's say I have a content type 'persons' (with some basic fields like name, job). Now I have a vocabulary 'teams' with terms like 'team a', 'team b'.
I use these terms in Views to group the persons of the same teams together. The fields are in a table style, with the terms as 'grouping field'. So I get something like this:
Team A
-person 1
-person 2

Team B
-person 3
-person 4

Now the problems is the terms (and therefore the groups with persons) I use for grouping are not ordered by their weight inside the vocabulary. Should this be possible, somehow? The terms should not be ordered alphabetically, but by weight.

ar-jan’s picture

I could even sort by term: weight, instead of using numbered terms and sorting by term name. Thanks dmnd!

dmnd’s picture

Hi Arjan,
that's great you got it to work? what version are you using?
Did you install cck taxonomy and taxonomy fields? Is that how you got it to work? I didn't try it yet.

ar-jan’s picture

I'm using Drupal 6.4, Views 6.x-2.0-rc3, CCK 6.x-2.0-rc7 and core Taxonomy. I tried a couple of months back and didn't get it to work then, what I did now was almost the same, as far as I can remember. Maybe they fixed / added something in Views...

I think the keys to getting it worked are like you mentioned: filter by taxonomy: term and select all needed terms from the vocab, then order it as the second filter.

vood002’s picture

I've been screwing around with making this work with computed field. It seems like Computed Field should be able to act on a node's taxonomy with something like this:

<?php

$lightest = 999999;
$vid = 12; //the vocabulary ID that you want to sort by

foreach ( $node->taxonomy as $term ){
	if ( ($term->vid == $vid ) && ( $term->weight < $lightest )  ){
		$lightest = $term->weight;
	}
}

$node_field[0]['value'] = $lightest;

?>

I'll admit that this didn't work for me. Perhaps computed field does not have access to $node->taxonomy when it performs it's computation? Either way, I had to change the code a little bit to draw the data from my CCK Taxonomy field like so:

<?php

$lightest = 999999;
foreach( $node->field_field_title as $equ ){
	if ($equ['value'] != ''){
		$term = taxonomy_get_term($equ['value']);
		if ( $term->weight < $lightest  ){
			$lightest = $term->weight;
		}
	}
}

$node_field[0]['value'] = $lightest;

?>

This works well for me for sorting in views.

dman’s picture

Old post, but if you come here searching (like I did)
If using views, Sort by term weight was helpful.