<p>Try something like this:</p>

<pre><code>{% set author = entry.metaAuthor|length ? entry.metaAuthor[0] : homepage_entry.metaAuthor[0] }}

&lt;meta name="author" content="{{ author }}" /&gt;
</code></pre>

CC BY-SA 3.0
2017-12-05T06:54:43.127Z
Hey Brad just a quick question. I know the `ElementCriteriaModel` has an iterable interface, that's why we can loop it to get the entries (by the way, why did you remove that in Craft 3?) but as far as I know the length filter does not change the type of the object (just caches element) and it stays an object. Could you please explain why this is possible since you would pull the first index of an object in your code. I know models have a `toString` to get the title but how can you even fetch the first element? I just want to learn how that works php wise. Thank you very much
Robin Schambach
CC BY-SA 3.0
2017-12-06T01:05:50.140Z
Calling `|length`, triggers `ElementCriteriaModel`'s `count()` method, which calls `total()`, which ultimately calls the database and it's no longer an `ElementCriteriaModel`, but an array of results from the database. Relational fields (entries, users, categories, tags, etc.), will always return an array of results, even if their is only one selected, which is why `metaAuthor[0]` works. Of course, this is all pointless in Craft 3 as ElementCriteriaModel is no longer a thing, too. :)
Brad Bell
CC BY-SA 3.0
2017-12-07T18:53:59.020Z
on Homepage getting error: `Impossible to access a key ("0") on a string variable ("Author Name").`
frshjb373
CC BY-SA 3.0
2017-12-07T23:37:53.777Z
@frshjb373 Can you update your original question with the code you're using when you get that error?
Brad Bell
CC BY-SA 3.0
2017-12-21T21:30:40.223Z
@BradBell, I have updated my question to include additional information.
frshjb373
CC BY-SA 3.0
2017-12-04T21:26:30.753Z
2017-12-04T21:26:30.753Z
Brad Bell
<p>After running Craft update I'm getting error on the meta author, meta description and meta keywords fields. Any idea how to resolve issue?</p>

<blockquote>
  <p>Recoverable error<br>
  Object of class Craft\ElementCriteriaModel could not be converted to string</p>
</blockquote>

<p>Error Output:
<a href="https://i.sstatic.net/zJUqD.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zJUqD.jpg" alt="enter image description here"></a></p>

<p><strong>Template Code:</strong></p>

<pre><code>{% set site_title = 'site title here' %}

{#
    ENTRY FALLBACK
    Not all pages have entries associated with them, e.g. search results, login page, etc.
#}
{% if entry.id is not defined %}
    {% set entry = craft.entries.id(2) %}
{% endif %}

{#
   HOMEPAGE DATA
   Used as a fallback for when meta values don't exist in this entry
#}
{% set homepage_entry = craft.entries.id(2) %}


&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
    &lt;title&gt;{{ entry.id == '2' ? site_title : entry.title ~ ' &gt; ' ~ site_title }}&lt;/title&gt;
    &lt;meta name="author" content="{{ entry.metaAuthor ? entry.metaAuthor : homepage_entry.metaAuthor }}" /&gt;
    &lt;meta name="description" content="{{ entry.metaDescription ? entry.metaDescription : homepage_entry.metaDescription }}" /&gt;
    &lt;meta name="keywords" content="{{ entry.metaKeywords ? entry.metaKeywords|join(',') : homepage_entry.metaKeywords|join(',') }}" /&gt;
    &lt;link rel="shortcut icon" href="images/favicon.ico" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="/css/all.css"&gt;
    &lt;link rel="stylesheet" type="text/css" href="/css/nav.css"&gt;
&lt;/head&gt;
</code></pre>

<p>Any idea what issue is, how to resolve and why this broke? </p>

CC BY-SA 3.0
2017-12-01T22:30:49.903Z
Is `metaAuthor` an entries field?
Brad Bell
CC BY-SA 3.0
2017-12-04T18:54:23.353Z
@BradBell, yes, it is. It was working fine before I updated to latest ver of Craft.
frshjb373
CC BY-SA 3.0
2017-12-21T21:50:30.093Z
You don't fetch the entries. You just prepare queries but you have to execute them with `.first()`
Robin Schambach
CC BY-SA 3.0
2017-12-22T18:39:00.733Z
yes, that did the trick.
frshjb373
<error><craft-pro><updates>
Recoverable error Object of class Craft\ElementCriteriaModel could not be converted to string after Update
# Recoverable error Object of class Craft\ElementCriteriaModel could not be converted to string after Update

(Asked by: username_0 on 2017-12-01)

After running Craft update I'm getting error on the meta author, meta description and meta keywords fields. Any idea how to resolve issue?

Recoverable error
  Object of class Craft\ElementCriteriaModel could not be converted to string

Error Output:

Template Code:
{% set site_title = 'site title here' %}

{#
    ENTRY FALLBACK
    Not all pages have entries associated with them, e.g. search results, login page, etc.
#}
{% if entry.id is not defined %}
    {% set entry = craft.entries.id(2) %}
{% endif %}

{#
   HOMEPAGE DATA
   Used as a fallback for when meta values don't exist in this entry
#}
{% set homepage_entry = craft.entries.id(2) %}





    
    
    {{ entry.id == '2' ? site_title : entry.title ~ ' > ' ~ site_title }}
    
    
    
    
    
    


Any idea what issue is, how to resolve and why this broke?

**Tags:** error, craft-pro, updates

> **Comments:**
> **username_1**: Is `metaAuthor` an entries field?  
> **username_0**: @BradBell, yes, it is. It was working fine before I updated to latest ver of Craft.  
> **username_2**: You don't fetch the entries. You just prepare queries but you have to execute them with `.first()`  
> **username_0**: yes, that did the trick.  

---

## 1 answers

### Answer by username_1 on 2017-12-04. Score: 1

Try something like this:
{% set author = entry.metaAuthor|length ? entry.metaAuthor[0] : homepage_entry.metaAuthor[0] }}

> **Comments:**
> **username_2**: Hey Brad just a quick question. I know the `ElementCriteriaModel` has an iterable interface, that's why we can loop it to get the entries (by the way, why did you remove that in Craft 3?) but as far as I know the length filter does not change the type of the object (just caches element) and it stays an object. Could you please explain why this is possible since you would pull the first index of an object in your code. I know models have a `toString` to get the title but how can you even fetch the first element? I just want to learn how that works php wise. Thank you very much  
> **username_1**: Calling `|length`, triggers `ElementCriteriaModel`'s `count()` method, which calls `total()`, which ultimately calls the database and it's no longer an `ElementCriteriaModel`, but an array of results from the database. Relational fields (entries, users, categories, tags, etc.), will always return an array of results, even if their is only one selected, which is why `metaAuthor[0]` works. Of course, this is all pointless in Craft 3 as ElementCriteriaModel is no longer a thing, too. :)  
> **username_0**: on Homepage getting error: `Impossible to access a key ("0") on a string variable ("Author Name").`  
> **username_1**: @username_0 Can you update your original question with the code you're using when you get that error?  
> **username_0**: @BradBell, I have updated my question to include additional information.  

---

