【发布时间】:2014-08-13 19:01:33
【问题描述】:
我使用taxonomy_select_nodes 通过传递分类 ID 来获取所有节点。我需要获取特定范围之间的节点。是否可以将日期范围传递给此函数?
谢谢!
【问题讨论】:
标签: drupal drupal-7 drupal-taxonomy
我使用taxonomy_select_nodes 通过传递分类 ID 来获取所有节点。我需要获取特定范围之间的节点。是否可以将日期范围传递给此函数?
谢谢!
【问题讨论】:
标签: drupal drupal-7 drupal-taxonomy
没有。您可以在 drupal 7 代码库中找到该函数:modules/taxonomy/taxonomy.module
如果您查看该函数,它不允许任何替代查询。
您可以将此函数复制到您的自定义模块中并重命名,例如“mymodule_select_nodes_by_date”,然后更改查询逻辑和输入参数。
function mymodule_select_nodes_by_date($tid, $pager = TRUE, $limit = FALSE, $start_date = "", $end_date = "", $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('tid', $tid);
if($start_date != "") {
$query->condition('t.created', $start_date, '>=');
}
if($end_date != "") {
$query->condition('t.created', $end_date, '<=');
}
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();
}
这需要 unix 时间戳输入,例如:
$data = mymodule_select_nodes_by_date(20, FALSE, FALSE, 1330970202, 1375200837);
如果您想使用不同格式的日期输入,您可以使用任何 php 的日期操作函数来获取格式化的日期,然后在查询之前将它们转换为 unix 时间戳。
【讨论】: