18 votes
Accepted

How can I cache a list of entries with pending postDates and have it automatically invalidate the cache appropriately?

Easy! Get the most soonest upcoming pending entry, and use until: {% set firstPending = craft.entries({ section: 'articles', status: 'pending', order: 'postDate asc' }).first %} {% set ...
user avatar
  • 3,051
12 votes
Accepted

How can I display a "pending" entry in my "_entry" template?

I'm assuming that when you defined your section, you checked the box which says this: Entries in this section have their own URLs ... and then filled out the relevant Entry URL Format and Entry ...
user avatar
  • 23.6k
9 votes
Accepted

Display Entries in a calendar

I haven't tested this, but it should be a good starting point, at the very least. {# Determine the number of days in the month #} {% set daysInMonth = "now"|date("t") %} {# Determine the date range ...
user avatar
8 votes
Accepted

No ordinary news post ordering

You can do this entirely from the order param using MySQL’s CASE syntax: {% set newsItems = craft.entries({ section: 'news', order: '(CASE field_myLightswitchField WHEN 1 ...
user avatar
8 votes
Accepted

How to filter entries by month and year?

You could try this (not tested): {% set month = craft.request.getParam('month') %} {% set year = craft.request.getParam('year') %} {% set firstDayOfMonth = now | date_modify('first day of ' ~ month ...
user avatar
  • 6,288
7 votes
Accepted

How to only show entries older than 12 months

You were close, you can set a postDate param on your craft.entries criteria model. And the Twig filter date_modify helps you to get the DateTime from 12 months ago. {% set timeAgo = now|date_modify('...
user avatar
  • 35.8k
7 votes
Accepted

Filter entries by month

The value you need to set the postDate parameter to is quite complex. If you define a dedicated variable for it, it makes the code more readable and you can more easily debug the value using the dump()...
user avatar
  • 35.8k
6 votes
Accepted

Adjust all date/times to users current timezone

I wouldn’t recommend hacking the MySQL timezone, since that would happen on a global basis, not just for the current user, and will result in some weird race conditions whenever two people are using ...
user avatar
5 votes
Accepted

Formatting Slug {postDate.month} to 2 characters

You should just be able to do: postDate|date('m') postDate by itself should give you a DateTime object which you can then apply the filter to.
user avatar
  • 4,509
5 votes

Creating a date filtered archive template

I've used this method for sorting by year and month (it could be expanded to support days). On the news/index.html template: {% if month is defined %} {# show an archive of a month #} {% set ...
user avatar
  • 2,975
5 votes
Accepted

How to translate de postdate format for a multilanguage site

Instead of: {{ entry.postDate.format('d M Y') }} Use: {{ entry.postDate | date('d M Y') }} That should work.
user avatar
  • 6,288
5 votes

How to set entries to auto-expire after certain # of days from post date?

There's no build in functionality for this, but you could make a custom plugin that automatically sets the expiration date expiryDate when you first save an entry with a specific entry type. Another ...
user avatar
  • 35.8k
4 votes

No ordinary news post ordering

I don't know if it's possible (but would like to see a query solution). There is one problem with your setup: If an entry gets a "substantial update", you can't make a correction afterwards, since it ...
user avatar
  • 8,316
4 votes
Accepted

Get entries by date range from plugin

Ok, found an answer that made it a bit clearer: How do I get elements using getCriteria from a specific date? Basically you can do this: $criteria = craft()->elements->getCriteria(ElementType::...
user avatar
  • 283
4 votes

Updating the postDate field from a plugin?

$timeslot isn't formatted correctly to be used as the postDate. I'd create a field that takes a date and a time and then use DateTimeHelper::formatTimeForDb() to format for use as postDate. You can't ...
user avatar
  • 3,833
4 votes
Accepted

Using user-selectable timezones to show localized post dates

It is possible to work around the Craft 2 bug, by converting the Craft DateTime object to a native one. {% set craftDateTime = entry.postDate %} {% set nativeDateTime = date(craftDateTime|date('c')) %...
user avatar
  • 35.8k
4 votes

Entry Post Date Format time since - X days ago

It looks as though Craft 3’s built-in duration Twig filter might help: Posted {{ entry.myDateField.diff(now) | duration }} ago. With a bit of additional logic, you can make this more useful, after ...
user avatar
4 votes

February always comes out as March - expect /yyyy/mm to be 2018/02 but it's 2018/03

I think the problem is that you are not being specific enough with the date that you are passing to the date function. You are in effect calling date("February") and the PHP date function has to make ...
user avatar
  • 166
3 votes
Accepted

Adding a postDate with Guest Entries

You can add a hidden field for your postDate to your GuestEntry form, e.g. <input type="hidden" name="postDate" value="{{ now|date("Y-m-j H:i:s") }}" /> This adds the postDate using Craft's ...
user avatar
3 votes
Accepted

CException Craft\MatrixBlockModel and its behaviors do not have a method or closure named "postDate"

Looks like maybe you have an Assets field within a Matrix field, which has the dynamic subpath set to: {postDate.year}/{postDate.month}/ But as the error suggests, Matrix blocks don’t have post ...
user avatar
3 votes
Accepted

Cached entries and postDate / expirationDate

Currently, it will ignore them and will only invalidate according to any explicit parameters you've told it to. However, I think it should take into account an entry's post and expiration date so I'...
user avatar
  • 66.4k
3 votes

No ordinary news post ordering

I think I'd create a twig function or filter to create a custom sort (usort in php), pass the collection to the function so it's returned 'sorted'. Would be interested in seeing if anyone has a nice ...
user avatar
3 votes
Accepted

Import Plugin date format (Post Date postDate)

I haven't yet used the plugin, but I'd try to import the dates into another, custom datetime field (e.g. myImportDate) to see if it's a problem with postDate being a special field (also goes into ...
user avatar
  • 35.8k
3 votes

No ordinary news post ordering

My SuperSort plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value. So, you ...
user avatar
  • 3,390
3 votes
Accepted

Showing posts live during a date range

You are passing a string <= startDate to the field parameter, but you actually want it to be a string <= 2016-01-01, which you can get by concatenationg the <= string with the datetime ...
user avatar
  • 35.8k
3 votes
Accepted

Entry Post Date Format time since - X days ago

Chiming in with a solution made from reading the above solution and comments. I also wrapped it in a macro. {% macro timeAgo(entry) %} {% set dateOneHourAgo = now.modify('- 1 hour') %} {% ...
user avatar
2 votes

Import Plugin date format (Post Date postDate)

Yes, thanks carlcs! So, as long as your postDate is set to this format "2014-07-31 12:00:00" and NOT your localised version as it appears in the Control Panel, everything works. Thanks to the magic ...
user avatar
2 votes

How to set entries to auto-expire after certain # of days from post date?

You could use my Auto Expire plugin for this. Just add a new expiration rule with the expiration date set to: { postDate|date_modify('+14 days 5am')|date('c') }
user avatar
  • 35.8k
2 votes

Display Entries in a calendar

Thanks for this @mmc501 I have adapted the code to work the wonderful sprig plugin. {% set time = time ?? "now" %} <div class="grid text-center"> <div class="col&...
user avatar
2 votes

Display Entries in a calendar

Just an update to show final current version of code for calendar - re stefan comment. {% if year is defined and month is defined %} {% set time = (year ~ "-" ~ month ~ "-" ~ "01") %} {%...
user avatar
  • 1,769

Only top scored, non community-wiki answers of a minimum length are eligible