Jump to content

Sort child excerpt by date


melissa_boyle
 Share

Recommended Posts

Hi Guys,

I am pretty new to process wire so apologises if this is a silly question.

Basically I have created a blog where it shows the excerpts of each blog post so to speak, my issue is that I cannot arrange these by date (most recent being at the top) using the blog_date field. I have read through similar questions however cannot implement my code to work.

Any help would be greatly appreciated,

Melissa

 <?php    
                
                foreach ($results as $child) {
                    echo "<div class='article'><div class='row'><div class='col'><h3>";
                    echo "$child->blog_title </h3> </div></div>";
                    echo "<div class='row'><div class='col twocol blog_meta'>$child->blog_date</div>";
                    echo "<div class='col eightcol post'><p>$child->blog_excerpt</p>";
                    echo "<a href='$child->url' class='read_more'>>> Continue Reading</a>";
                    echo "</div></div></div>";
                }
            ?>
 
Link to comment
Share on other sites

maybe $results->sort("-blog_date");

see http://cheatsheet.processwire.com/

$a->sort("property")

Sorts the WireArray by the given item property (string). You may also specify the property as "property.subproperty", where property resolves to a Wire derived object, and subproperty resolves to a property within that object. Prepend a minus sign "-" to the property name reverse the sort.

  • Like 1
Link to comment
Share on other sites

Also, you ideally want to sort these before they come out of the database rather than after. That happens in your $pages->find() or $page->children() or whatever code comes before the code you posted that populates $results. i.e. 

$results = $pages->find("template=blog, sort=-blog_date, limit=3"); 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...