1

I was wondering if it's possible to modify the status of an Entry within a plugin?

I've tried setting the status property but I get a readonly error.

$entry->status = $status
5

Not directly, but you can indirectly change it. An entry's status is defined by several attributes.

An entry is considered "live" if it is enabled (both on a per-locale and global basis), it has a post date in the past and an expiration date that doesn't exist or hasn't been met yet.

If the entry is enabled and the post date is greater than the current date, then the entry's status is "pending".

If it is enabled and the current date is past the expiration date, then it has a status of "expired".

If an entry is not enabled at all, it has a status of "disabled".

  • Hi Brad, thank you for your help. Setting enabled to 0 achieves what I'm trying to for making it 'not live' however, setting enabled to 1 doesn't make the entry 'live'. Any suggestions for how to go to about that? – user4041 Oct 1 '15 at 9:38
  • All of the other conditions I listed above have to be met before it becomes "live". – Brad Bell Oct 1 '15 at 15:26
  • When I set the enabled to 0, none of the properties you mention above (barring enabled of course) seem to change. The post date is in the past and the expiration date is blank. So, as expected, the entry status is disabled. With the properties still in the same state (post date in the past and no expiration date) setting enabled to 1 isn't changing the status to disabled, so is there something I'm missing? – user4041 Oct 2 '15 at 9:06
  • Just read this again, meant to say setting enabled to 1 isn't changing the status to enabled, so is there something I'm missing? – user4041 Oct 30 '15 at 11:16
4

This isn't exactly an answer, but more of an extension of Brad's answer. The docs define what an entries status can be. So as Brad was saying the status itself cannot be set, but:

if the entry is enabled + has valid post date + has not expired = status is enabled.

I found your post because I was also setting enabled->true but my entry was not getting set to enabled. Then I realize...make sure you save the entry after you have updated it's status.

In my case, I wanted to get all entries related to an authorId. So here's what it looks like:

MyPlugin.php

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'my-section-handle';
$criteria->authorId = $user->id;
$criteria->status = null;  // status is null to find all entries regardless of status

$entries = $criteria->find();

if ($entries) {
    foreach ($entries as $entry) {
        $entry->enabled = true;

        if (craft()->entries->saveEntry($entry)) {
            // Entry updated & saved.
        } else {
            // Could not save entry.
        }
    }
} else {
    // No entry found for that user.
}

Hope this helps!

  • 1
    Thanks for the tip Damon :) I actually did have it saving in both cases (setting enabled to true or false) and it still wasn't setting it properly. My solution, which I don't like and can imagine is against Craft recommendations, is to modify the column directly using Yii. It's currently working but I intend to get to the bottom of why the preferred method isn't – user4041 Dec 9 '15 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy