1

I want to list nodes from children terms using or editing taxonomy_select_nodes function. Actually I added these few lines but I get an infinite loop or slow response:

function taxonomy_select_nodes_ext($tid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'),$promote) { 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'); // This block of code is the custom code I added. //SELECT only promoted article if($promote==1){ $query->join('node', 'no', 'no.nid = t.nid'); $query->condition('promote', 1); } $taxonomies=array(); $taxonomies = taxonomy_get_children($tid); $taxonomies[]=$tid; $query->condition('t.tid', $taxonomies,'IN'); 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(); } 
2
  • Can you post all of your code instead of just that one part. We can't tell what the problem is with only part of the code. For example, you have a */ to end a comment but the comment isn't started anywhere and there is no start to or execution of the $query. Commented Nov 26, 2014 at 11:37
  • I put the full function Commented Nov 27, 2014 at 13:08

1 Answer 1

0

I would say these are your problems:

taxonomy_get_children() returns term objects, not term IDs, so that is going to break that query. This I would say is your main issue. Instead you could do this:

 $taxonomies = array(); $taxonomies = taxonomy_get_children($tid); // Get the child term tids. $taxonomies = array_keys($taxonomies); // Add the parent tid. $taxonomies[] = $tid; $query->condition('t.tid', $taxonomies,'IN'); 

Your IN statement with the children won't work currently because earlier in the function is this line:

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

So that will only ever return values for $tid, never for the children unless you remove that line also.

Also I would recommend giving the new $promote parameter a default value, like this:

function taxonomy_select_nodes_ext($tid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'), $promote = 0) { 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.