Blog

Code Examples for Statamic Entries

Statamic

Query all entries of a collection

use Statamic\Facades\Entry;

Entry::query()->where("collection", "posts")->get();

In this example, the Entry facade of Statamic is used to query all entries (entries) in a specific collection (collection). Here, all entries of the posts collection are searched for. The get() method executes the query and returns the results.

Query all entries of a user in a collection

use Statamic\Facades\Entry;
use Statamic\Facades\User;

$author = User::findByEmail('user@example.com');
 
Entry::query()->where('collection', 'pages')->where('author', $author->id())->get();

In this example, the Entry and User facades from Statamic are imported first. Then, a specific user (user) is queried via their email address. Afterwards, the Entry::query() method is used to find all entries (entries) in a specific collection (collection), in this case pages, that were created by this user. The get() method executes the query and returns the results.

Query all entries with a specific blueprint

use Statamic\Facades\Entry;

Entry::query()->where('blueprint', 'page')->get();

In this example, the Entry facade is used to find all entries (entries) with a specific blueprint. A blueprint in Statamic defines the data structure of content. Here, all entries with the page blueprint are searched for. The get() method executes the query and returns the results.

Add term to an entry via code

use Statamic\Facades\Entry;
use Statamic\Facades\Term;

// Query term
$tag = Term::query()->where('taxonomy', 'tags')->where('title', 'Tag A')->first();

// Query entry
$entry = Entry::query()->where('title', 'Test post')->first();

// Query existing terms
$tags = $entry->get('tags');

// Add desired term
$tags[] = $tag->slug;

// Remove duplicates
$tags = array_values(array_unique($tags));

// Set terms
$entry->set('tags', $tags);

// Save entry
$entry->save();

In this example, a specific term (term), in this case a tag, is queried from the 'tags' taxonomy. Afterwards, a specific entry (entry) is queried. Then, the current tags of the entry are retrieved. The newly queried tag is added to the list of current tags. Duplicates are removed with array_values(array_unique($tags)). Subsequently, the updated tags are assigned to the entry with set(). Finally, the entry with the updated tags is saved with save().

Duplicate entry

The following code allows you to duplicate an existing entry. It should be noted that date() can only be used with collections that have enabled publication date.

$clone = Entry::make()
	->collection($entry->collection())
	->locale($entry->locale())
	->slug($entry->slug()) // must be adjusted
	->date($entry->date())
	->data($entry->data()->all());

$clone->save()

Duplicate all entries of a collection into another language

The following code is designed for a multisite website. First, all German pages are queried, then iterated over these pages, and each of these pages is cloned. The slug of each page is extended by the prefix 'en-', in addition, the German version of the page is set as the origin for the English version:

use Statamic\Facades\Entry;

$germanPages = Entry::query()
    ->where("collection", "pages")
    ->where("locale", "de")
    ->get();

foreach ($germanPages as $germanPage) {
    $clone = Entry::make()
        ->collection($germanPage->collection())
        ->locale('en')
        ->origin($entry->id)
        ->blueprint($germanPage->blueprint())
        ->slug('en-'.$germanPage->slug())
        ->data($germanPage->data()->all());

    $clone->save();
}

Emptying an entire collection

To empty an entire collection, the following line of code can be used. In this example, the collection pages is emptied:

Collection::findByHandle('pages')->truncate();

One has to be careful, especially if the collection has a structure and can thus contain subpages. The above line of code would delete one page after another, which is why the home page would disappear first. The next page automatically becomes the home page. If a page with a subpage becomes the home page, the deletion process stops, as the home page cannot have subpages. In this case, a different code has to be used, which in this case deletes all German pages of a multisite:

use Statamic\Facades\Collection;

$reverseGermanPages = Collection::findByHandle('pages')->structure()->in('de')->flattenedPages()->reverse();
$reverseGermanPages->each(function ($page) {
    $page->delete();
});

First, the page structure is queried and then reversed. In other words - the pages are then deleted in reverse order (from bottom to top). This prevents the above-described problem of the home page having subpages.