具有日期范围的taxonomy_select_nodes - drupal 7

时间:2014-08-13 19:01:34

标签: drupal drupal-7 drupal-taxonomy

我使用taxonomy_select_nodes通过传递分类标识来获取所有节点。我需要在特定范围之间获取节点。是否可以将日期范围传递给此函数?

谢谢!

1 个答案:

答案 0 :(得分:1)

没有。你可以在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时间戳。