How to select only recent posts

Selecting recent posts in CCS was easy. eg
[loop type=post count=5 category=notices after=‘8 weeks ago’]

I’ve got the loop working in L&L except I cannot get “after=‘8 weeks ago’]” working.

My latest attempt, which the editor says is wrongly formatted, is:

 <If <Date date_format=timestamp>-8 weeks</Date> before=<Field publish_date />>

So,

  1. I cannot see how to code the “date 8 weeks ago” inside an IF statement, and
  2. What formatting of dates do I need for the “before=” to work?

Any ideas or suggestions please.

Hi William, sorry for the late reply!

About querying for posts by published date, there’s an attribute called publish_date.

<Loop type=post count=5 category=notices publish_date="8 weeks ago" publish_compare="after">

I think the attribute publish_compare is necessary, because without it the query would search for posts published exactly 8 weeks ago, on that specific day.

Maybe the Loop tag should accept an attribute called after, like in CCS, as a short way to express the publish date query.


About passing the result of Date tag in a tag attribute, there’s a special syntax which replaces <> with {}.

<If check="{Field publish_date}" after="{Date format=timestamp}-8 weeks{/Date}">

…Actually, for the above condition, there’s a better way. There’s a field attribute to compare a field value, and the after attribute converts to timestamp already.

<If field="publish_date" after="-8 weeks">

These date conditionals are described here:

But for efficiency and performance, it’s better to query using Loop tag attributes instead of filtering with If tag.

Thank you Eliot. From reading “Date Conditionals” I had arrived at the clumsy:

<Loop type=post count=4 order=desc orderby=date category=notices>
<If check="{Date subtract='2 months' format=timestamp}" before="{Field publish_date date_format=timestamp}" >

But following your reply I now have the much better "<If loop exists " which lets me nicely wrap any posts if they exist:

<If loop exists type=post count=5 category="notices" publish_date="2 months ago" publish_compare="after" order=desc orderby=publish_date>
  <section class=box>
  <Loop>
        .....
  </Loop>
  </section>
</If>
2 Likes