Answers.Body: <p>You need to set an array of category IDs to your categories field.</p>
  
  <pre><code>$featuredEntry-&gt;setContentFromPost(array(
      'category' =&gt; array(42)
  ));
  </code></pre>
  
  <p>Query for it with an <code>craft.categories</code> criteria model or if you're hardcoding it anyways you could just look it up in the CP:</p>
  
  <blockquote>
    <p>example.com/admin/categories/myCategoryGroup/<strong>42</strong>-myCategoryHandle</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-26T09:01:31.903Z
Answers.Text: cheers @carlcs perfect solution
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-24T17:13:12.720Z
Answers.LastActivityDate: 2015-08-24T17:13:12.720Z
Answers.UserDisplayName: carlcs
Body: <p>Ok I'm buliding a plugin which changes the category of a news article if another article is set to the same category so that only one article is set as the featured category.</p>
  
  <p>So far I have the onSaveBeforeEntry event working ok I'm just stuck on the updating the existing featured article. This is the flow so far followed by the code I'm having an issue with.</p>
  
  <ol>
  <li>First check the article being saved to see if the category is set.</li>
  <li>If it is check it has not changed.</li>
  <li>If it has changed and it has changed to 'Featured' use getCriteria to find the entry that is currently set to 'Featured'</li>
  <li>(the sticking point) set the existing featured entry category to 'Latest' and save it.</li>
  <li>Finish off saving the entry.</li>
  </ol>
  
  <p>Point 4 is not changing the existing featured article data.</p>
  
  <p>The code I have is:</p>
  
  <pre><code>// Get the current featured news entry
  $entry_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $entry_criteria-&gt;section = 'news';
  $entry_criteria-&gt;search = 'category:Featured';
  $featuredEntry = $entry_criteria-&gt;first();
  
  // update exiting featured article and mark as latest
  $featuredEntry-&gt;setContentFromPost(array('category' =&gt; 'Latest'));
  $success = craft()-&gt;entries-&gt;saveEntry($featuredEntry);
  </code></pre>
  
  <p>Getting the existing entry works, changing the category and re-saving does not.</p>
  
  <p>What am I missing?</p>
  
  <p>Any help appreciated.</p>
  
  <p>Ta</p>
  
Tags: <plugin-development><plugins>
Title: Changing an entries data in onBeforeSaveEntry event depending on data set in saved entry
ThreadText: # Changing an entries data in onBeforeSaveEntry event depending on data set in saved entry
  
  (Asked by: username_0 on 2015-08-24)
  
  Ok I'm buliding a plugin which changes the category of a news article if another article is set to the same category so that only one article is set as the featured category.
  
  So far I have the onSaveBeforeEntry event working ok I'm just stuck on the updating the existing featured article. This is the flow so far followed by the code I'm having an issue with.
  
  1. First check the article being saved to see if the category is set.
  2. If it is check it has not changed.
  3. If it has changed and it has changed to 'Featured' use getCriteria to find the entry that is currently set to 'Featured'
  4. (the sticking point) set the existing featured entry category to 'Latest' and save it.
  5. Finish off saving the entry.
  
  Point 4 is not changing the existing featured article data.
  
  The code I have is:
  
  ```
  // Get the current featured news entry
  $entry_criteria = craft()->elements->getCriteria(ElementType::Entry);
  $entry_criteria->section = 'news';
  $entry_criteria->search = 'category:Featured';
  $featuredEntry = $entry_criteria->first();
  
  // update exiting featured article and mark as latest
  $featuredEntry->setContentFromPost(array('category' => 'Latest'));
  $success = craft()->entries->saveEntry($featuredEntry);
  
  ```
  
  Getting the existing entry works, changing the category and re-saving does not.
  
  What am I missing?
  
  Any help appreciated.
  
  Ta
  
  **Tags:** plugin-development, plugins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-08-24. Score: 1 | Accepted answer
  
  You need to set an array of category IDs to your categories field.
  
  ```
  $featuredEntry->setContentFromPost(array(
      'category' => array(42)
  ));
  
  ```
  
  Query for it with an `craft.categories` criteria model or if you're hardcoding it anyways you could just look it up in the CP:
  
  > example.com/admin/categories/myCategoryGroup/**42**-myCategoryHandle
  
  > **Comments:**
  > **username_0**: cheers @username_1 perfect solution  
  
  ---
  
  