欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

function taxonomy_select_nodes

shiping1 的头像

function taxonomy_select_nodes

7 taxonomy.moduletaxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'))
4.6 taxonomy.moduletaxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE)
4.7 taxonomy.moduletaxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC')
5 taxonomy.moduletaxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC')
6 taxonomy.moduletaxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC')
8 taxonomy.moduletaxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'))

Finds all nodes that match selected taxonomy conditions.

Parameters

$tids: An array of term IDs to match.

$operator: How to interpret multiple IDs in the array. Can be "or" or "and".

$depth: How many levels deep to traverse the taxonomy tree. Can be a nonnegative integer or "all".

$pager: Whether the nodes are to be used with a pager (the case on most Drupal pages) or not (in an XML feed, for example).

$order: The order clause for the query that retrieve the nodes.

Return value

A resource identifier pointing to the query results.

1 call to taxonomy_select_nodes()

File

 

modules/taxonomy/taxonomy.module, line 1183
Enables the organization of content into categories.

Code

function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC') {
  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 = db_placeholders($args, 'int');
      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid 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.vid = tn.vid 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.vid = tn' . $index . '.vid';
        $wheres .= ' AND tn' . $index . '.tid IN (' . db_placeholders($tids, 'int') . ')';
        $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;
}

Comments

Hi,

why is there a limit when using pager?

The description should mention it... This function doesn't fetch all the nodes but only a number of node, depending on the fact that there's a pager or not.

Regards,
cfab

Annoyingly, even if you're not using a pager, it still only selects a range of nodes - only limited to "feed_default_items" instead of "default_nodes_main"...

function select_nodes_by_content_type_not_in_vocabulary($vocab_id,$content_type){
$result = db_query(
   "SELECT * FROM {node} WHERE nid NOT IN (
SELECT nid FROM {term_node} AS tn
LEFT JOIN {term_data} AS td ON tn.tid = td.tid
WHERE td.vid = %d)
AND type = '%s'",
$vocab_id , $content_type
);
return $result;
}

example:

//vid 2 is colors
$widgets_with_no_color_yet= select_nodes_by_content_type_not_in_vocabulary(2,"widget");
while ($sub_obj = db_fetch_object($widgets_with_no_color_yet)) {
   $this_widget = node_load($sub_obj->nid);
   echo("This widget doesn't have a color set yet: ".l($this_widget->title);
}

Adapted from comments on this page: http://drupal.org/node/89879

hello

Using the function I realize that I show all the nodes, the limit imposed on:

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));
     }

the default_nodes_main has a maximum of 30 in drupal to the main page, this I think is a problem if you want to display more nodes, it would be nice to add the argument to the function so that when applying this value can be handled, I think good, thanks for this function is excellent to me saved my life.

Here's a simple function to retrieve all nodes associated with a group of tids from the same vocabulary:

function _taxonomy_select_nids_per_vocab($tids = array(), $separator = ',') {
  //list of allowed separator for the nids string.
  $allowed_separators = array(',', ' ', '.', '-');
 
  if (!$tids || !is_array($tids) || empty($tids))
    return;

  if (!in_array($separator, $allowed_separators))
    return;

  $sql = "SELECT GROUP_CONCAT(nid SEPARATOR '{$separator}') as 'nids'
          FROM {term_node} tn INNER JOIN {term_data} td ON tn.tid = td.tid WHERE tn.tid IN (". implode(',', $tids) .") GROUP BY td.vid";

  $nids = db_fetch_object(db_query($sql))->nids;
  return $nids;
}

Returns a string with all nids separated by separator char.

i have written a custom query to select nodes with access check
for huge term ids > 3000 term ids.

<?php
   
//term ids to select nodes for
   
$alltermids = array(1,2,3,........,3000)
   
  
$grants = array();
    foreach (
node_access_grants($op, $account) as $realm => $gids) {
      foreach (
$gids as $gid) {
       
$grants[] = "(gid = $gid AND realm = '$realm')";
      }
    }

   

$grants_sql = '';
    if (
count($grants)) {
     
$grants_sql = 'AND (' . implode(' OR ', $grants) . ')';
    }

  

$op = 'view';
 
  
$sql = "SELECT title FROM {node_access} AS na
           INNER JOIN (
            SELECT n.title AS title, n.nid AS nid  FROM {node} AS n
            WHERE nid IN (
                SELECT nid
                FROM {term_node} tn
                INNER JOIN {term_data} td ON tn.tid = td.tid
                WHERE tn.tid IN ("
. implode(',', $alltermids) .")
            )
          )
          res ON res.nid = na.nid
          WHERE res.nid
$grants_sql AND grant_$op >= 1";

    

//fetch first result
    
(while .....)
    
$title = db_fetch_object(db_query($sql))->title;
?>

<?php
function mymodule_select_nodes($tids = array(), $order = 'n.sticky DESC, n.created DESC') {
  if (
count($tids) > 0) {
   
$placeholders = db_placeholders($tids, 'int');
   
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ') AND n.status = 1 ORDER BY ' . $order;
   
$sql = db_rewrite_sql($sql);
   
$result = db_query($sql,$tids);
  }
  return
$result;
}

?>
来自 https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_select_nodes/6

 
普通分类: