0
$results = db_query("select nid from {taxonomy_index} where tid = :tid", array(':tid' => arg(2)));
foreach ($results as $result) {
  $node = node_load($result->nid);
  echo $node->title;
}

I put the code in page--taxonomy.tpl.php, but there is no result. Why?

The output of print_r($results) is the following one.

DatabaseStatementBase Object
(
    [dbh] => DatabaseConnection_mysql Object
        (
            [shutdownRegistered:protected] => 
            [target:protected] => default
            [key:protected] => default
            [logger:protected] => 
            [transactionLayers:protected] => Array
                (
                )

            [driverClasses:protected] => Array
                (
                    [SelectQuery] => SelectQuery
                )

            [statementClass:protected] => DatabaseStatementBase
            [transactionSupport:protected] => 1
            [transactionalDDLSupport:protected] => 
            [temporaryNameIndex:protected] => 0
            [connectionOptions:protected] => Array
                (
                    [database] => 
                    [username] => 
                    [password] => 
                    [host] => 
                    [port] => 
                    [driver] => 
                    [prefix] => Array
                        (
                            [default] => 
                        )

                )

            [schema:protected] => 
            [prefixes:protected] => Array
                (
                    [default] => 
                )

            [prefixSearch:protected] => Array
                (
                    [0] => {
                    [1] => }
                )

            [prefixReplace:protected] => Array
                (
                    [0] => 
                    [1] => 
                )

        )

    [queryString] => select nid from taxonomy_index where tid = :tid
0

Please confirm you got any value from arg(2) which is equivalent to term id(tid) .

Try this code

$result = db_select('taxonomy_index', 't')
  ->fields('t')
  ->condition('tid', arg(2), '=')
  ->execute()
  ->fetchAssoc();

$node = node_load($result->nid);

print_r($node);
| improve this answer | |
0

The taxonomy_index database table just contains a list of the nodes using a taxonomy term, not that taxonomy term, and any of its child terms (or its parent terms). If there isn't any node that is using a taxonomy term, the code you wrote doesn't print anything.

If you want to get a list of node using a taxonomy term, or its child terms, you could use code similar to the following one.

$tid = arg(2);
$query = db_select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query->addField('t', 'tid');
$query->condition(
  db_or()
    ->condition('h.parent', $tid)
    ->condition('t.tid', $tid)
);
$query->addTag('term_access');
$query->orderBy('t.weight');
$query->orderBy('t.name');
$tids = $query->execute()->fetchCol();

$nids = db_query("select nid from {taxonomy_index} where tid IN :tids", array(':tids' => $tids))->fetchCol();

$nodes = node_load_multiple($nids);

The other reasons that could cause the code not to print anything are:

  • The taxonomy_index table is empty for some reasons, including the fact the Drupal variable taxonomy_maintain_index_table is not set to TRUE. Check what variable_get('taxonomy_maintain_index_table') returns; if it returns FALSE (or an equivalent value), that would explain why the table is not populated.
  • The value you are using as taxonomy term ID is not a taxonomy term ID.
  • There isn't any node using the taxonomy term for which you have the ID.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.