Answers.Body: <p>You need to create the field in the CMS, however you don't need to assign it to any section. It should still be available to your plugin and twig templates.</p>
  
  <p>The other reason your code will result in an error is because <code>$entry-&gt;secretName</code> is in fact being processed by the <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#__get-detail" rel="nofollow noreferrer">BaseElementModel magic getter method</a>, so is the same as writing <code>$entry-&gt;getFieldValue('secretName')</code>, which obviously cannot be set to a new value.</p>
  
  <p>The proper way to do this is using the <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail" rel="nofollow noreferrer">setContent method</a> as follows:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'dogs';
  $entries = $criteria-&gt;find();
  
  foreach ($entries as $entry) {
      $entry-&gt;setContent(array(
          'secretName' =&gt; urlencode($entry-&gt;title)
      ));
      craft()-&gt;entries-&gt;saveEntry($entry);
  endforeach;
  </code></pre>
  
  <p>See <a href="https://craftcms.stackexchange.com/a/4487/180">this answer</a> for a more thorough explanation on setting entry content.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-05T22:58:08.703Z
Answers.Text: I didn't know that you could set/access fields not assigned to the section's field Layout. That's good to know!
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-05T22:47:09.560Z
Answers.LastActivityDate: 2015-02-05T22:54:33.407Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: Ben Croker
Body: <p>In a plugin I am creating I want to add a property to some entries.</p>
  
  <p>Ideally this property would be hidden from the admin user, as I don't want them to edit it.</p>
  
  <p>At the moment I am trying to do something like this (simplified):</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;section = 'dogs';
  $entries = $criteria-&gt;find();
  
  foreach ($entries as $entry) {
      $entry-&gt;secretName = urlencode($entry-&gt;title);
      craft()-&gt;entries-&gt;saveEntry($entry);
  endforeach;
  </code></pre>
  
  <p>But this produces the error 'Property "Craft\EntryModel.secretName" is not defined.'</p>
  
  <p>Does anyone know how to do what I am trying to do? Is there a way to set a new property to just this section?</p>
  
Tags: <plugin-development><entrymodel>
Title: Is there a way to set a new property on an entry
ThreadText: # Is there a way to set a new property on an entry
  
  (Asked by: username_0 on 2015-02-05)
  
  In a plugin I am creating I want to add a property to some entries.
  
  Ideally this property would be hidden from the admin user, as I don't want them to edit it.
  
  At the moment I am trying to do something like this (simplified):
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->section = 'dogs';
  $entries = $criteria->find();
  
  foreach ($entries as $entry) {
      $entry->secretName = urlencode($entry->title);
      craft()->entries->saveEntry($entry);
  endforeach;
  
  ```
  
  But this produces the error 'Property "Craft\EntryModel.secretName" is not defined.'
  
  Does anyone know how to do what I am trying to do? Is there a way to set a new property to just this section?
  
  **Tags:** plugin-development, entrymodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-02-05. Score: 5 | Accepted answer
  
  You need to create the field in the CMS, however you don't need to assign it to any section. It should still be available to your plugin and twig templates.
  
  The other reason your code will result in an error is because `$entry->secretName` is in fact being processed by the [BaseElementModel magic getter method](http://buildwithcraft.com/classreference/models/BaseElementModel#__get-detail), so is the same as writing `$entry->getFieldValue('secretName')`, which obviously cannot be set to a new value.
  
  The proper way to do this is using the [setContent method](http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail) as follows:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'dogs';
  $entries = $criteria->find();
  
  foreach ($entries as $entry) {
      $entry->setContent(array(
          'secretName' => urlencode($entry->title)
      ));
      craft()->entries->saveEntry($entry);
  endforeach;
  
  ```
  
  See [this answer](https://craftcms.stackexchange.com/a/4487/180) for a more thorough explanation on setting entry content.
  
  > **Comments:**
  > **username_2**: I didn't know that you could set/access fields not assigned to the section's field Layout. That's good to know!  
  
  ---
  
  