Entry Lookup

Search for and retrieve entries in you twig templates using the entry lookup. Perform a lookup by calling enact.entry and defining a method chain of property/value pairs specifying the conditions under which to lookup entries. Every property defined by the Entry Wrapper is available as a method on the lookup, call the property you want to use as a condition, passing it a value. By default each value is compared to the property by being exactly equal (in SQL just property=value), but you can prefix your values with the below comparators to specify a different comparison (always include a space after the comparator):

  • > - Where property is greater than the value.

  • < - Where property is less than the value.

  • >= - Where property is greater than or equal to the value.

  • <= - Where property is less than or equal to the value.

  • ! - Where the property does not equal the value.

  • LIKE - Where the property contains the value.

Unless otherwise specified, only published entries are returned from entry lookups.

Basic example

//get all the entries in the products section

{% set products = enact.entry.sectionHandle('products').fetch %}

When you are done specifying your conditions, call the method fetch to return the array of matching entries, each represented as a \Enact\template\wrapper\Entry. You may also use the first and last method to retrieve the first or last entry returned from the lookup. Both methods use the fetch method internally.

Loop the results

<p>{{ products|length }} products</p>

<ul>

{% for product in products %}

    <li>{{ product.getLinkHtml }} - {{ product.createdOn }}</li>

{% endfor %}

</ul>

More examples

//get entries in 'Art Pieces' section that were created within the last week

{% set oneWeekAgo = 'now'|date_modify('-1 week')|date('Y-m-d') %}

{% set entries = enact.entry.sectionName('Art Pieces').createdOn('> ' ~ oneWeekAgo).fetch %}


//get entries that have been updated within the last week

{% set entries = enact.entry.lastEditedOn('> ' ~ oneWeekAgo).fetch %}


//get entries who have parent entries and are at a depth of 3

{% set entries = enact.entry.parentId('! null').atDepth(3).fetch %}


//get pending entries (NOTICE: unless specified only published entries are returned from lookups)

{% set entries = enact.entry.status('pending').fetch %}

 

Order method

Use the order method to sort the result of the lookup, passing a property name and and optional sort type (ASC, DESC), by default ASC is used.

{% set entries = enact.entry.search('mustang').sectionHandle('cars').order('createdOn DESC').fetch %}
Limit method

Limit the number of entries returned by the lookup

{% set entries = enact.entry.search('mustang').sectionHandle('cars').limit(10).fetch %}
Search method

Use the search method to perform a LIKE comparison on the title and url properties as well as all fields values used by the entry.

{% set cars = enact.entry.search('mustang').sectionHandle('cars').fetch %}

When the search method is used a special field property is added to entries named score which scores the match of the search. Use this in conjunction with the order method to sort searched entries based on their score.

{% set mustangs = enact.entry.search('mustang').order('score DESC').limit(10) %}
Page method

Use the page method in conjunction with the limit method to return a specific number of entries from a specific page of the results.

// will return entries 20-40

{% set blogs = enact.entry.sectionHandle('blog').order('createdOn DESC').limit(20).page(2).fetch %}
Total method

Use the total method to force the lookup to return an integer value specifying the number of entries that matched the condition. The same can be accomplished using the twig length filter, however, this prevents the actual entries from being fetched and returned, saving database resources.

{% set totalEntries = enact.entry.sectionHandle('blog').total.fetch %}
Field method

Use the field method to specify the fields (identified by their handles) that should be attached and populated on the entries returned by the lookup. By default all fields of each entry are attached and populated when doing lookups, use this method to override that (this is only warranted when you want to save system/database resources). The field method accepts as arguments an array of field handles, a comma delimited string of field handles, or multiple handles passed as individual arguments.

{% set blogs = enact.entry.sectionHandle('blog').field('body,promoVideo').fetch %}
Tag & TagId method

Use the tag or tagId method to force the lookup to only return entries that are using the tag defined by the tag handle passed to tag or the tags unique ID passed to tagId.

{% set blogs = enact.entry.sectionHandle('blog').tag('breakingNews').order('createdOn DESC').fetch %}