You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/5.x/reference/element-types/entries.md
+69Lines changed: 69 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -470,6 +470,75 @@ Entries can be copied and pasted between sections and fields that support the gi
470
470
471
471
<Seepath="../../system/elements.md"hash="copying-elements"label="Copying Elements"description="Learn how to copy entries and other elements between contexts." />
472
472
473
+
## Statuses
474
+
475
+
In addition to the [base element statuses](../../system/elements.md#statuses), entries may appear in any of these states:
476
+
477
+
-**Pending** (`pending`) — The **Post Date** is in the future, or not set.
478
+
-**Expired** (`expired`) — The **Expiry Date** is set, and in the past.
479
+
-**Live** (`live`) — The **Post Date** is in the past, and the **Expiry Date** is in the future (if set at all). This is the default status used in entry queries.
480
+
481
+
::: tip
482
+
Status pips and controls may be hidden from element edit screes, indexes, chips, and cards, if an entry’s type has the **Show the Status field** setting _off_.
483
+
:::
484
+
485
+
When [querying entries by status](../../system/elements.md#querying-by-status), keep in mind that each status identifier in the list above represents multiple conditions. Statuses and their default query conditions help make the appearance or availability of entries consistent between the control panel and front-end; for this reason, params like `.postDate()` and `.expiryDate()` introduce _additional_ constraints, rather than replacing Craft’s. If you need full control over the query constraints, call `.status(null)`.
486
+
487
+
### Static Statuses <Sincever="5.7.0"feature="Static storage of entry statuses" />
488
+
489
+
Craft’s powerful [events](../../extend/events.md) system allows developers to react to element updates, and connect author activity and content to other systems. Statuses, however, can be difficult to pin down—entries rely heavily on their **Post Date** and **Expiry Date** attributes to determine when an entry is returned, and what its “status” is at any given time.
490
+
491
+
Take this query that fetches blog articles:
492
+
493
+
```twig
494
+
{% set posts = craft.entries()
495
+
.section('blog')
496
+
.all() %}
497
+
```
498
+
499
+
Suppose we have an article with a **Post Date** five minutes in the future. Loading the page with this query _now_ would not display the pending article; if we wait five minutes and refresh the page, the entry _does_ show up… without any intervention by the author! This is because each query compares the server’s current time with entries’ stored `postDate` and `expiryDate` columns; depending on when you look at the data, Craft considers it to be in different statuses. Therein lies the issue: nothing _happens_ when a pending entry “goes live” (or a live entry “expires”), so Craft can’t emit status-change events.
500
+
501
+
The <config5:staticStatuses> config setting alters this behavior: instead of calculating statuses on-the-fly, Craft stores a static representation of an entry’s status at the time it is saved. Entry queries also use simplified conditions, matching against the stored values instead of date-based comparisons. In this mode, however, entries’ statuses only change when they are saved. The accompanying [`update-statuses` console command](../cli.md#update-statuses) provides a means of routinely checking and re-saving entries:
502
+
503
+
```bash
504
+
ddev craft update-statuses
505
+
```
506
+
507
+
::: warning
508
+
If you opt in to static statuses, you must run this command with a tool like [cron](https://en.wikipedia.org/wiki/Cron) to ensure your content is published and expired in a reasonable amount of time.
509
+
:::
510
+
511
+
In a [private plugin](../../extend/plugin-guide.md) or [custom module](../../extend/module-guide.md), you can [listen for save events](kb:handling-entry-saves) and check whether an entry received a new status:
512
+
513
+
```php
514
+
use craft\base\Event;
515
+
use craft\elements\Entry;
516
+
use craft\events\ModelEvent;
517
+
use craft\helpers\ElementHelper;
518
+
519
+
Event::on(
520
+
Entry::class,
521
+
Entry::EVENT_AFTER_SAVE,
522
+
function (ModelEvent $event) {
523
+
/** @var Entry $entry */
524
+
$entry = $event->sender;
525
+
526
+
// Make sure we’re dealing with a canonical entry, and that there was indeed a status change:
Depending on your editorial process, you will need to adjust the cases here to handle different transitions. Keep in mind that this handler may also run in response to normal entry saves (during an HTTP request) so it is advisable to offload any time- or resource-intensive operations to a [queue job](../../extend/queue-jobs.md).
541
+
473
542
## Querying Entries
474
543
475
544
When Craft receives a request matching an entry’s URI, it automatically makes an `entry` variable available. Everywhere else in your front-end (or PHP code), you can fetch entries using **entry queries**.
0 commit comments