1

I'm building a JSON array of several thousand entries using the following entries query:

{% set services = craft.entries.section('services').asArray().all() %}

The problem is there some private info is visible within the JSON and can be viewed in the browser source (not to mention the result is HUGE.

let services = {{ services | json_encode | raw }};

Is there a parameter similar to ids() where I can specify only the title, url, and ID? If not, what's the best Twig way to reduce this array to what I'm looking for?

3

As per the docs:

Element queries are specialized query builders under the hood, so they support most of the same methods provided by craft\db\Query.

...So, in your case you could use select() to only select the fields you want:

{% set services = craft.entries.section('services').asArray().select(['title','id','url']).all() %}

1
  • Thanks! I knew this was possible. The .select() example on the page just takes you to the Yii docs. I ws adding select() before the asArray(). – simplethemes Aug 31 '20 at 3:03
1

I ended up using:

{% set svcarray = craft.entries.section('services').select('content.id,content.title,elements_sites.slug,elements_sites.uri').asArray().all() %}

For whatever reason this ended up throwing an exception:

{% set svcarray = craft.entries.section('services').asArray().select(['title','id','url']).all() %}

Integrity constraint violation: 1052 Column 'id' in field list is ambiguous

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.