Answers.Body: <p>The problem is that your query is only finding enabled entries. <a href="https://craftcms.com/docs/4.x/entries.html#querying-entries" rel="nofollow noreferrer">Entry queries</a> set some parameters by default - in particular, it won't return disabled or expired entries unless explicitly instructed to do so. That's because you don't want disabled entries to show up in your queries for the frontend by default.</p>
  <p>You can control that behaviour using the <a href="https://craftcms.com/docs/4.x/entries.html#status" rel="nofollow noreferrer"><code>status</code> parameter</a>. The default value <code>'live'</code> will only find entries that are enabled and not expired. If you want to find <em>all</em> entries regardless of status, you can remove the <code>status</code> parameter by passing <code>null</code>:</p>
  <pre class="lang-php prettyprint-override"><code>$entries = Entry::find()
      -&gt;sectionId(1)
      -&gt;status(null)
      -&gt;all();
  </code></pre>
  <hr />
  <p>As a sidenote: Hardcoding a <code>sectionId</code> is not a good idea. Section IDs are unique in every environment, so this code might break if deployed in the production environment. Using section handles (<code>-&gt;sectionId('myCustomSection')</code> is safe because handles are part of the project config, so they are identical in every environment.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-26T08:42:13.683Z
Answers.Text: Ah...face palm. Of course, totally blanked on the status part. Thank you!
  
  And good point on the `sectionId` too. I've always gone to IDs as they feel somewhat more reliable than a handle, but you're right - that thinking breaks down across environments.
Answers.UserDisplayName: supazu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-26T08:24:10.260Z
Answers.LastActivityDate: 2023-06-26T08:24:10.260Z
Answers.UserDisplayName: MoritzLost
Body: <p>I'm creating to updating the title of some entries via a content migration, but this apparently doesn't work on disabled entries. So I'm trying to enable the entries to start, but that is also having no effect.</p>
  <p>Does anyone know why <code>$entry-&gt;enabled = true;</code> wouldn't work? Full code below.</p>
  <pre><code>public function safeUp(): bool {
    $entries = Entry::find()-&gt;sectionId(1)-&gt;all();
    foreach ($entries as $entry) {
      if (!$entry-&gt;enabled) {
        $entry-&gt;enabled = true;
        Craft::$app-&gt;getElements()-&gt;saveElement($entry);
      }
  
      // Do the title updating stuff
    
      $entry-&gt;title = $updatedTitle;
      Craft::$app-&gt;getElements()-&gt;saveElement($entry);
    }
    return true;
  }
  </code></pre>
  
Tags: <migration>
Title: Programatically enable an entry
ThreadText: # Programatically enable an entry
  
  (Asked by: username_0 on 2023-06-26)
  
  I'm creating to updating the title of some entries via a content migration, but this apparently doesn't work on disabled entries. So I'm trying to enable the entries to start, but that is also having no effect.
  
  Does anyone know why `$entry->enabled = true;` wouldn't work? Full code below.
  
  ```
  public function safeUp(): bool {
    $entries = Entry::find()->sectionId(1)->all();
    foreach ($entries as $entry) {
      if (!$entry->enabled) {
        $entry->enabled = true;
        Craft::$app->getElements()->saveElement($entry);
      }
  
      // Do the title updating stuff
    
      $entry->title = $updatedTitle;
      Craft::$app->getElements()->saveElement($entry);
    }
    return true;
  }
  
  ```
  
  **Tags:** migration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-06-26. Score: 2 | Accepted answer
  
  The problem is that your query is only finding enabled entries. [Entry queries](https://craftcms.com/docs/4.x/entries.html#querying-entries) set some parameters by default - in particular, it won't return disabled or expired entries unless explicitly instructed to do so. That's because you don't want disabled entries to show up in your queries for the frontend by default.
  
  You can control that behaviour using the [`status` parameter](https://craftcms.com/docs/4.x/entries.html#status). The default value `'live'` will only find entries that are enabled and not expired. If you want to find *all* entries regardless of status, you can remove the `status` parameter by passing `null`:
  
  ```php
  $entries = Entry::find()
      ->sectionId(1)
      ->status(null)
      ->all();
  
  ```
  
  ---
  
  As a sidenote: Hardcoding a `sectionId` is not a good idea. Section IDs are unique in every environment, so this code might break if deployed in the production environment. Using section handles (`->sectionId('myCustomSection')` is safe because handles are part of the project config, so they are identical in every environment.
  
  > **Comments:**
  > **username_0**: Ah...face palm. Of course, totally blanked on the status part. Thank you!  And good point on the `sectionId` too. I've always gone to IDs as they feel somewhat more reliable than a handle, but you're right - that thinking breaks down across environments.  
  
  ---
  
  