Skip to content

Query WordPress posts, pages, custom post types, meta, taxonomies and users as Eloquent models in a Pollora project with pollora/colt.

pollora/colt is a set of Eloquent models over the WordPress tables: posts, pages, custom post types, meta, taxonomies, terms, comments, menus, options and users. In a Pollora project it is already installed, and you use the Pollora\Models\* models (Post, Page, Term, User…), which extend Colt’s.

use Pollora\Models\Post;
$posts = Post::published()->latest('post_date')->take(20)->get();

A query returns an Eloquent Collection, and every type is a subclass of it, so map(), filter(), pluck() and the rest keep working:

ModelsCollection
Posts (and pages, custom post types)PostCollection
Menu items ($menu->items)MenuItemCollection
Terms and commentsMetaSiblingsCollection

The collection is what lets Colt load what a loop will need once for the whole set, instead of once per item.

Reading a relation of each item in a loop used to cost one or more queries per item. Colt now loads them once per collection, with no change to the API. Measured on 100 posts, each with its own author, image, terms and comments:

What you doQueries beforeQueries after
Post::get()->toArray() or toJson()5027
Read $post->terms (or main_category, keywords) in a loop2024
Read $post->image in a loop3025
$menuItem->instance() and $menuItem->parent() in a loop over $menu->items2046
$term->meta or $comment->meta in a loop over a collection2012

Serialising posts loads the image, terms, main_category and keywords attributes once for the whole collection. Menu items resolve their targets with one query per target class. Meta is still not loaded, nor serialised, until something reads it. A second call to instance() on the same menu item queries again, as before.

Colt cannot batch the lazy Eloquent relations author, comments, taxonomies and thumbnail: reading one in a loop is one query per post, as with any Eloquent model. Name them with with() when you read them in a loop:

$posts = Post::published()
->with('author', 'comments', 'taxonomies')
->take(100)
->get();
foreach ($posts as $post) {
echo $post->author->display_name, ' ', $post->comments->count();
}

That is 12 queries for 100 posts, whatever the number of posts.