0

I want to set up a View that shows all the nodes that were updated within 7 days of the node that is being viewed

So if the node being viewed has a post date of January 15, 2014, I would see posts that were published between January 8-15, 2014.

I know there is a contextual filter involved, but it eludes me how to do this so the date used is relative to the node it is being viewed on.

0

No idea if possible in Views, but I created a custom block to achieve this.

function relative_posts_block_info() {
  $blocks['relative_posts'] = array(
    'info' => t('Relative Posts'),
  );

  return $blocks;
}

function relative_posts_block_view($delta = '') {
  switch ($delta) {
    case 'relative_posts':
    $block['subject'] = t('Block Title Goes Here');

    $node = menu_get_object();
    $formatted_date = date('m/d/Y H:i:s', $node->created);
    $start_date = strtotime('+2 weeks', strtotime($formatted_date));
    $end_date = strtotime('-2 weeks', strtotime($formatted_date));

    $query = new EntityFieldQuery();
    $entities = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'story')
    ->fieldCondition('taxonomy_vocabulary_1', 'tid', $node->taxonomy_vocabulary_1['und'][0]['tid'])
    ->propertyCondition('created', array($end_date, $start_date), 'BETWEEN')
    ->propertyOrderBy("created", "DESC")
    ->propertyCondition('status', 1)
    ->range(0, 25)
    ->execute();

   $block['content'] = '<ul>';

  foreach($entities['node'] as $obj) {
  $node = node_load($obj->nid);
    $block['content'] .= '<li>' . l($node->title, "node/" . $node->nid) . '</li>';
  }
  $block['content'] .= '</ul>';

   break;
 }

 return $block;
}    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.