I use Taxonomy Theme for different vocabularies.

Under one vocabulary I want the nodes to be ordered alphabetically under each term. So in the template.php of that particular theme I have added this:

function themename_taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.title ASC') {
  if (count($tids) > 0) {
    // For each term ID, generate an array of descendant term IDs to the right depth.
    $descendant_tids = array();
    if ($depth === 'all') {
      $depth = NULL;
    }
    foreach ($tids as $index => $tid) {
      $term = taxonomy_get_term($tid);
      $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
    }

    if ($operator == 'or') {
      $args = call_user_func_array('array_merge', $descendant_tids);
      $placeholders = implode(',', array_fill(0, count($args), '%d'));
      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1 ORDER BY '. $order;
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1';
    }
    else {
      $joins = '';
      $wheres = '';
      $args = array();
      foreach ($descendant_tids as $index => $tids) {
        $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
        $placeholders = implode(',', array_fill(0, count($tids), '%d'));
        $wheres .= ' AND tn'. $index .'.tid IN ('. $placeholders .')';
        $args = array_merge($args, $tids);
      }
      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY '. $order;
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres;
    }
    $sql = db_rewrite_sql($sql);
    $sql_count = db_rewrite_sql($sql_count);
    if ($pager) {
      $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
    }
    else {
      $result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
    }
  }

  return $result;
}

This, of course, I have taken from the core taxonomy module.

However, this does not seem to work. The nodes are still ordered by "n.created".

Where am I going wrong?

--

Subir

Comments

jnvsor’s picture

5.x eh?

That's tough, with 6.x I would have just enabled a view and changed the order...

What you are doing here is creating a function... This particular function states that the default for $order is n.title ASC, but unless the drupal team screwd up somewhere, that function WILL be called with a custom variable... n.created DESC

There are 2 choices, you could put

<?php
$order = "n.title ASC";
?>

at the beginning of the function, to overide it, or you could delve into the wonderfull but complicated world of hooks and change it in there... Unfortunately, I never bothered to learn about hooks, so I'm afraid you're on your own...

nevets’s picture

You can not just override any function which is why that does not work.