Filter Posts By Date

The following examples will show you how to display Posts on the website based on their date.

These examples are useful for a situation where Posts are used to advertise events on the website, and you need to hide the Post after a certain date / time.

To change the Date of a Post, navigate to Dashboard > Website > Posts, click a Post to edit, and select the "additional attributes" tab. 

Step One:
Use an <data:repeater> to retrieve Posts associated with a Post Category, and output the Posts's date and time:

<data:repeater as="post" datasource="\Components\Website\Posts::getAllForCategory(1)"> <p> <strong>Title:</strong> [? $post['post_title'] ?]<br> <strong>Unformatted Date:</strong> [? $post['post_date'] ?]<br> <strong>Formatted Date:</strong> [? formatDate($post['post_date']) ?]<br> <strong>Date &amp; Time:</strong> [? formatDateTime($post['post_date']) ?] </p> </data:repeater>


Step Two:

Add an <logic:if> control, to test whether the Post Date is greater than the current date / time:

<data:repeater as="post" datasource="\Components\Website\Posts::getAllForCategory(1)"> <logic:if test="$post['post_date'] > date('Y-m-d H:i:s')"> <p> <strong>Title:</strong> [? $post['post_title'] ?]<br> <strong>Time:</strong> [? formatDateTime($post['post_date']) ?] </p> </logic:if> </data:repeater>


Step Three:

Extending this, we can slightly alter the <logic:if> test, so that Posts display 24 hours after the Post's Date:

<data:repeater as="post" datasource="\Components\Website\Posts::getAllForCategory(1)"> <logic:if test="$post['post_date'] > date('Y-m-d H:i:s', strtotime('-24 hours'))"> <p> <strong>Title:</strong> [? $post['post_title'] ?]<br> <strong>Time:</strong> [? formatDateTime($post['post_date']) ?] </p> </logic:if> </data:repeater>

Change the order Posts are displayed

By default, Posts will be ordered by the Post Date in descending order. To reverse this, update the dataorder="" attribute of the <data:repeater>.:

<data:repeater as="post" datasource="\Components\Website\Posts::getAllForCategory(1)" dataorder="[? [['post_date', 'asc']] ?]"> <logic:if test="$post['post_date'] > date('Y-m-d H:i:s', strtotime('-24 hours'))"> <p> <strong>Title:</strong> [? $post['post_title'] ?]<br> <strong>Time:</strong> [? formatDateTime($post['post_date']) ?] </p> </logic:if> </data:repeater>

Use datafilter to improve performance

In the situation where you website contains a lot of Posts (noticeable at > 100 Posts), you will see improved performance by using the datafilter="" attribute on the <data:repeater>, which would replace the need for the <logic:if> test: 

<data:repeater as="post" datasource="\Components\Website\Posts::getAllForCategory(1)" datafilter="[? [['post_date', '&gt;', date('Y-m-d H:i:s', strtotime('-24 hours'))]] ?]" dataorder="[? [['post_date', 'asc']] ?]"> <p> <strong>Title:</strong> [? $post['post_title'] ?]<br> <strong>Time:</strong> [? formatDateTime($post['post_date']) ?] </p> </data:repeater>

Use the Post Template Display to finish

Instead of just displaying the Post Title and Date, use the Post Display instead:

<data:repeater as="post" datasource="\Components\Website\Posts::getAllForCategory(1)" datafilter="[? [['post_date', '&gt;', date('Y-m-d H:i:s', strtotime('-24 hours'))]] ?]" dataorder="[? [['post_date', 'asc']] ?]"> <data:template component="\Components\Website\Posts" templatetype="list" /> </data:repeater>