Same name and namespace in other branches

Return nodes attached to a term across all field instances.

This function requires taxonomy module to be maintaining its own tables, and will return an empty array if it is not. If using other field storage methods alternatives methods for listing terms will need to be used.

Parameters

$tid: The term ID.

$pager: Boolean to indicate whether a pager should be used.

$limit: Integer. The maximum number of nodes to find. Set to FALSE for no limit.

$order: An array of fields and directions.

Return value

An array of nids matching the query.

1 call to taxonomy_select_nodes()

File


  • modules/

    taxonomy/

    taxonomy.module, line 204

  • Enables the organization of content into categories.


Code

function taxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $order = array(
  't.sticky' => 'DESC',
  't.created' => 'DESC',
)) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }
  $query = db_select('taxonomy_index', 't');
  $query
    ->addTag('node_access');
  $query
    ->condition('t.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');
  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();
}

Comments

elvis2’s picture

This function really needs to take language into account.

phpawy’s picture

I needed to chose nodes while adding a condition for language
here is my edited function:

/**
* Return nodes attached to a term across all field instances.
*
* This function requires taxonomy module to be maintaining its own tables,
* and will return an empty array if it is not. If using other field storage
* methods alternatives methods for listing terms will need to be used.
*
* @param $tid
* The term ID.
* @param $pager
* Boolean to indicate whether a pager should be used.
* @param $limit
* Integer. The maximum number of nodes to find.
* Set to FALSE for no limit.
* @param $order
* An array of fields and directions.
* @param $language
* a 2 letter string representing language wanted
*
* @return
* An array of nids matching the query.
*/
function taxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'), $language=false) {
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');
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);
}
if($language)
{
$query->join('node', 'no', 'no.nid = t.nid');
$query->condition('language', $language);
}
return $query->execute()->fetchCol();
}

phpawy’s picture

also you should update function taxonomy_term_page in file: taxonomy.pages.inc accordingly to list nodes for selected language only

/**
 * @file
 * Page callbacks for the taxonomy module.
 */

/**
 * Menu callback; displays all nodes associated with a term.
 *
 * @param $term
 *   The taxonomy term.
 * @return
 *   The page content.
 */
function taxonomy_term_page($term) 
{
  // If there is a menu link to this term, the link becomes the last part of
  // the active trail, and the link name becomes the page title. Thus, we must
  // explicitly set the page title to be the term title.
  drupal_set_title($term->name);

  // Build breadcrumb based on the hierarchy of the term.
  $current = (object) array(
    'tid' => $term->tid,
  );
  // @todo This overrides any other possible breadcrumb and is a pure hard-coded
  //   presumption. Make this behavior configurable per vocabulary or term.
  $breadcrumb = array();
  while ($parents = taxonomy_get_parents($current->tid)) {
    $current = array_shift($parents);
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  }
  global $language;

  $breadcrumb[] = l(t('Home'), NULL);
  $breadcrumb = array_reverse($breadcrumb);
  drupal_set_breadcrumb($breadcrumb);
  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  // Set the term path as the canonical URL to prevent duplicate content.
  $uri = entity_uri('taxonomy_term', $term);
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  // Set the non-aliased path as a default shortlink.
  drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);

  // Normally we would call taxonomy_term_show() here, but for backwards
  // compatibility in Drupal 7 we do not want to do that (it produces different
  // data structures and HTML markup than what Drupal 7 released with). Calling
  // taxonomy_term_view() directly provides essentially the same thing, but
  // allows us to wrap the rendered term in our desired array structure.
  $build['term_heading'] = array(
    '#prefix' => '<div class="term-listing-heading">',
    '#suffix' => '</div>',
    'term' => taxonomy_term_view($term, 'full'),
  );

  if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10), array('t.sticky' => 'DESC', 't.created' => 'DESC'), $language->language)) 
  {
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes);
    $build['pager'] = array(
      '#theme' => 'pager',
      '#weight' => 5,
    );
  }
  else {
    $build['no_content'] = array(
      '#prefix' => '<p>',
      '#markup' => t('There is currently no content classified with this term.'),
      '#suffix' => '</p>',
    );
  }
  return $build;
}
hanzouti’s picture

I want to add promote to sort node with this condition.

hanzouti’s picture

Here's the solution. I've added before the function returns the fetched data:

  //SELECT only promoted article
  if($promote==1){
	  $query->join('node', 'no', 'no.nid = t.nid');
	  $query->condition('promote', 1);
  }
tamerzg’s picture

Be careful when calling this function with default parameters.
For example for following code I would expect to return all nodes that have this $tid. However function returns only 10 nodes!
taxonomy_select_nodes($tid);

After further debugging it seems that its second param $pager is by default set to TRUE. Altough documentation on this page says that third param $limit is set to FALSE for no limit, one would expect that it will still return all nodes.
However you have to explicitly disable the pager like this:

taxonomy_select_nodes($tid, FALSE);

gbaudoin’s picture

Also fell into this trap. Why should it have a pager by default ? This is counter-intuitive at best. taxonomy_select_nodes($tid) should do that, and not assume I want it paged !

hanzouti’s picture

What Kind of tweak can we make to select nodes inside selected term and keeping the order by created. I tried this but it doesn't work properly:

          $taxonomies=array($tid);
	  $taxonomies = $taxonomies+taxonomy_get_children($tid);
	  $query->condition('t.tid', $taxonomies,'IN');
saads90’s picture

Link to a patch to use offset as a parameter to this function.

https://www.drupal.org/node/2639842

ouelmart’s picture

Be aware, if you have OG install, it will filter the nodes not in the user groups. It will return the nodes that have no group or are in the group of the "global $user;".


来自  https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_select_nodes/7.x