Search results post date?

Permalink
I've set up a blog on the site I'm building, and the search results page returns a list of searched posts, but the posts don't have post dates associated with them.

I've inserted this into the core Search block...

<?php echo $datePublic = date('F j, Y', strtotime($page->getCollectionDatePublic())); ?>


...but the dates returned are all the same date... is there a way I can make the dates be associated with the post date of individual posts?

Thanks!

 
bbeng89 replied on at Permalink Reply
bbeng89
Keep in mind that the results returned by the search results are not actually Page objects, but rather are instances of the IndexedSearchResults class. This class has a method getDate() which should do what you want. So if you're making a custom template, inside the for loop you could do:

foreach($results as $r){
    //... 
    echo $r->getDate('F j, Y');
}


If you want the actual page object you just have to pull it like so:

foreach($results as $r){
    $page = Page::getByID($r->getID());
    //do something with page
}


Hope that helps!