Hi all, I have a CCK node with a bunch of fields. I also have a simple module which I use only for a few different views. E.g.


function msaextras_menu() {

	$items = array();
	
	$items[] = array('path' => 'credits', 'title' => t('credits'), 'callback' => 'msaextras_credits', 'access' => true, 'type' => MENU_CALLBACK);
	$items[] = array('path' => 'filmography', 'title' => t('filmography'), 'callback' => 'msaextras_filmography', 'access' => true, 'type' => MENU_CALLBACK);
	$items[] = array('path' => 'films', 'title' => t('films'), 'callback' => 'msaextras_films', 'access' => true, 'type' => MENU_CALLBACK);
	...
	...
	return $items;
}


function msaextras_credits() {
	
	$content = '';

	$vid = 1; // credits category
	$terms = taxonomy_get_tree($vid);
	
	$content .= "<p>&nbsp;</p>- <a href='/rogereaton_credits.pdf' target='_blank'>Download PDF</a> -";
	foreach($terms as $term) {
		$content .= "<h1 class='subheading'>$term->name</h1>";
		$nodes = taxonomy_select_nodes(array($term->tid), 'or', 'all', true, $order = 'n.sticky DESC, n.created DESC');
		while ($n = db_fetch_object($nodes)) {
			$content.= node_view(node_load($n->nid), false);
		}
	}
	
	print theme("page", $content);
}

My problem is I want to sort by a field which is a CCK field. I was wondering how I could do that? I tried the views module and that can sort by CCK fields, but unfortunately I cant use the views module as I couldnt get it to group the nodes by category the way I wanted to. And also here I simplify my code by simply using node_view, whereas actually I have quite a few different ways of formatting the node which I do in the module, not in the node.tpl...

but anyways, any suggestions welcome!

cheers,

Comments

high1memo’s picture

Does no one have any ideas on this?

Michael Phipps’s picture

I know I'm digging up a really old thread, and the question was based on old 4.7 code, but since I've done some time looking for a solution in D6 I'm posting what I've found here. In my case, I wanted to sort by a last name cck field in a content type called profile.

First, the query in taxonomy_select_nodes() function does not contain any joins to cck fields, so you'll need to rewrite that function.

The way to do this without hacking core is to copy the function from taxonomy.module into a custom module (or in your template.php file) and give it another name. eg: special_taxonomy_select_nodes()

Next, you need to add an extra join to connect to your cck table. Get the name of the cck table in the database that contains the field you want to sort on.

now find the line in the copy of the function taxonomy_select_nodes() that looks a bit like this:

$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.vid = tn'. $index .'.vid';

under that line add the join to the cck field (in this case I'm wanting to sort by a cck field called

$joins .= ' INNER JOIN {content_type_profile} p ON n.nid = p.nid';

Finally, modify this line:

$nodes = taxonomy_select_nodes(array($term->tid), 'or', 'all', true, $order = 'n.sticky DESC, n.created DESC');

to

$nodes = special_taxonomy_select_nodes(array($term->tid), 'or', 'all', true, $order = 'n.sticky DESC, p.field_lastname_value, n.created DESC');

Please note, you will probably need to go a bit further and check node types so you aren't trying to sort nodes that don't include the cck field you want to sort on.

I hope this gives someone a starting point. Please post corrections if I've done something really stupid!