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.
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
This function really needs to
This function really needs to take language into account.
Here is the language aware version of this function
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();
}
also you should update
also you should update function taxonomy_term_page in file: taxonomy.pages.inc accordingly to list nodes for selected language only
How can I add promote in the array of field
I want to add promote to sort node with this condition.
Join inner with the condition of promote=1
Here's the solution. I've added before the function returns the fetched data:
Be careful this function returns only 10 results
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);
Also fell into this trap. Why
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 !
How to select nodes from chrildren of the selected term?
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:
Offset as a parameter
Link to a patch to use offset as a parameter to this function.
https://www.drupal.org/node/2639842
organic groups
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