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.
- First check the article being saved to see if the category is set.
- If it is check it has not changed.
- If it has changed and it has changed to 'Featured' use getCriteria to find the entry that is currently set to 'Featured'
- (the sticking point) set the existing featured entry category to 'Latest' and save it.
- 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.