2

I am trying to display only the most read content from two months ago.

I have tried checking through mod_article_popular/helper.php file but can't figure out how to use the $model->setState to get what I want.

How can I get only articles within a date range to display?

3 Answers 3

3

You should be looking at ContentModelArticles class for which filter states to manipute. inside the classes getStoreId method is the answer:

protected function getStoreId($id = '')
{
    // Compile the store id.
    $id .= ':' . serialize($this->getState('filter.published'));
    $id .= ':' . $this->getState('filter.access');
    $id .= ':' . $this->getState('filter.featured');
    $id .= ':' . serialize($this->getState('filter.article_id'));
    $id .= ':' . $this->getState('filter.article_id.include');
    $id .= ':' . serialize($this->getState('filter.category_id'));
    $id .= ':' . $this->getState('filter.category_id.include');
    $id .= ':' . serialize($this->getState('filter.author_id'));
    $id .= ':' . $this->getState('filter.author_id.include');
    $id .= ':' . serialize($this->getState('filter.author_alias'));
    $id .= ':' . $this->getState('filter.author_alias.include');
    $id .= ':' . $this->getState('filter.date_filtering');
    $id .= ':' . $this->getState('filter.date_field');
    $id .= ':' . $this->getState('filter.start_date_range');
    $id .= ':' . $this->getState('filter.end_date_range');
    $id .= ':' . $this->getState('filter.relative_date');

    return parent::getStoreId($id);
}

So, the two filter state you need to set states for are:

    $model->setState('filter.date_filtering', $range);

FYI, it sounds like you are editing the core files directly; which is not recommended since any subsequent Joomla update could over write any customization.

Hope this helps...

3
  • Thanks. That helped a lot. I had a problem getting it to work though. After some trial and errors. I got it to work by adding $model->setState('filter.date_filtering', 'range'); before the code. You can make this edit so that I can mark your response as the correct answer.
    – Josh
    Oct 8, 2014 at 5:23
  • Done, thanks for the feed back. Oct 8, 2014 at 17:56
  • Ok. But you removed the other two lines. They are the correct filters to set but won't work without the line I provided. I'll just write out my discoveries as answers below. In case someone else needs them. Thanks again. You saved me a lot of time.
    – Josh
    Oct 8, 2014 at 21:10
3

I found two methods of solving this.

.1) Set a date backwards, relative to the current date. (I'm using 60 because I need articles from 2months back)

        $model->setState('filter.date_filtering', 'relative');
        $model->setState('filter.relative_date', $params->get('relative_date', 60)); 

        $items = $model->getItems();

2) Set any preferred date range

    $model->setState('filter.date_filtering', 'range');
    $start_date = strtotime('-2 months');
    $start_date = date("Y-m-d h:i:s", $start_date);
    $end_date = strtotime('now');
    $end_date = date("Y-m-d h:i:s", $end_date);        
    $model->setState('filter.start_date_range', $start_date);
    $model->setState('filter.end_date_range', $end_date);

    $items = $model->getItems();
1

A free module is available at JED that does exactly this. Most Popular Content by Date Range lets you set different date ranges for the popular articles, with options for last day, last 7 days, last 30 days and All time.

Most Popular Content by Date Range module

2
  • Thanks. But I have already customized the most read module to display images (which it doesn't do by default) + the one you recommended doesn't display images so I still have to do some customization on it + it is a joomla 3 module and I'm on joomla 2.5 ......
    – Josh
    Oct 15, 2014 at 11:41
  • No problem, I see you managed to get your code working, thanks for posting the code. I just wanted to mention this module as it might be useful for other users.
    – johanpw
    Oct 15, 2014 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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