I've searched a simple dynamic method to sort taxonomy pages on 'created by'.

I public my solution here for others;

edit modules/taxonomy.module and add the following lines

at the beginning of the function taxonomy_page() write
$taxonomy->order = arg(4);

and at the beginning of the function taxonomy_select_nodes($taxonomy, $pager = 1) write

$order = "DESC";
if ($taxonomy->order == "ASC" {
  $order = "ASC";
}

now replace the SQL Strings in this function. You must replace the $sql = ...ORDER BY static DESC, created DESC"; with ...ORDER BY static DESC, created $order";. There are two sql strings.

After you made these changes, you can change the sort order of a taxonomy page with a link like this;
http://www.yourpage.yourdomain/?q=taxonomy/page/or/28/ASC
or
http://www.yourpage.yourdomain/?q=taxonomy/page/or/28,40,20/ASC

Comments

Bèr Kessels’s picture

Punchy,

This looks very usefull indeed. I think that if you made a patch out of it and placed it in drupal's issue tracker, it would make a fair chance of getting in drupal (propably not 4.5 though).

Some small questions: does this also work for the 'and'? your examples only show 'or'.
Do the feeds follow these sort orders too?

[Ber | Drupal Services webschuur.com]

punchy-1’s picture

I've tested it with the 'and' and it works for this also.
http://www.mypage.ch/?q=taxonomy/page/and/22,28/ASC

What you mean exactly with the 'feeds'?

And nedjo; thanks for opening the issue.

nedjo’s picture

Thanks for the tips. I've created an issue at http://drupal.org/node/10839 about node sort order in taxonomy display.

boreg’s picture

Hi all,

i think i have solution for this issue and u need not to patch taxonomy module. I did it with overriding the theme_taxonomy_term_page() fucntion.
1) So in my template.php file, i created my own phptemplate_taxonomy_term_page() based on that one from core module.
2) i changed the bottom code $output .= taxonomy_render_nodes($result); to $output .= _modified_taxonomy_render_nodes($result);
3) i created new function _modified_taxonomy_render_nodes() based on the taxonomy_render_nodes(), but i created in it an array (in while cycle) to save node nid and title in that array, so i can then sort the array by a title.

This is my cation:

function phptemplate_taxonomy_term_page($tids, $result) {
.
.
.
  #$output .= taxonomy_render_nodes($result);  // uncomment
  $output .= _modified_taxonomy_render_nodes($result); // modified function
  return $output;
}


function _modified_taxonomy_render_nodes($result) { 
  $output = '';
  $has_rows = FALSE;
  while ($node = db_fetch_object($result)) {
    $boregnode[$node->nid] = $node->title;  // save nid and title to the array
    #$output .= node_view(node_load($node->nid), 1);  // uncomment cause of performance
    $has_rows = TRUE;
  }
  asort($boregnode);  // sort array by title
  foreach($boregnode as $key => $val) { // walk throw array and load nodes
    $output .= node_view(node_load($key), 1);
  }
  
  if ($has_rows) {
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
  }
  else {
    $output .= '<p>'. t('There are currently no posts in this category.') .'</p>';
  }
  return $output;
}

Hope it ll help u - sorry guys for my english..its terrible, i know :\

alunyov’s picture

I guess I'm looking for same feature. But your solution is not correct for me. Cause $result contains defined number of nodes (by default it is 10) and you parse and order nodes only for current page. So it doesn't work for multi page output of result correctly.

WilliamB’s picture

I wanted the node to be displayed in the "modified" order. The most recent modificated one on top.

I know it's not "good" to modify core module function but didn't find any other way, since i didn't want to add yet another module.

So i modified the taxonomy_select_nodes function in /modules/taxonomy/taxonomy.module file.

I added a join just before the foreach loop adding the order by:
$query->join('node', 'n', 'n.nid = t.nid');
That way i join my "taxonomy_index" table, used by the taxonomy module which only has the created date, with the "node" table. The join being on the node id.

And i changed the parameter that was passed in the function from
't.created' => 'DESC' to 'n.changed' => 'DESC'
That way i make the order by the "changed" column from the "node table" date instead of "created" column from the "taxonomy_index" table.

So it's a really quick change.

Here the full function after edit:

function taxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 'n.changed' => 'DESC')) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }
  $query = db_select('taxonomy_index', 't');

  $query->addTag('node_access');

  $query->condition('tid', $tid);

  if ($pager) {
    $count_query = clone $query;
    $count_query->addExpression('COUNT(t.nid)');

    $query = $query->extend('PagerDefault');
    if ($limit !== FALSE) {
      $query = $query->limit($limit);
    }
    $query->setCountQuery($count_query);
  }
  else {
    if ($limit !== FALSE) {
      $query->range(0, $limit);
    }
  }
  $query->addField('t', 'nid');

  $query->addField('t', 'tid');

  $query->join('node', 'n', 'n.nid = t.nid');

  foreach ($order as $field => $direction) {
    $query->orderBy($field, $direction);
    // ORDER BY fields need to be loaded too, assume they are in the form
    // table_alias.name
    list($table_alias, $name) = explode('.', $field);
    $query->addField($table_alias, $name);
  }
  
  return $query->execute()->fetchCol();
}
ikpoe’s picture

I have similar problem. Since I don't want add another module I modified the core module (the taxonomy_select_nodes function in /modules/taxonomy/taxonomy.module file) same as WilliamB.
Is there any other way that is better or more elegant?