Skip to content

Querying models ​

Each model is an Eloquent query builder: all the Eloquent methods (where, whereIn, orderBy, with, chunk...) are available. On top of that, the library provides a few shortcuts for the WordPress tables.

Find one model ​

Eloquent does not provide magic findOneBy* methods, but some models have dedicated static methods:

MethodColumn
Post::findOneByName($name)post_name
Post::findOneByGuid($guid)guid
User::findOneByEmail($email)user_email
User::findOneByLogin($login)user_login
Option::findOneByName($name)option_name

They return the model, or null if nothing matches.

php
use Dbout\WpOrm\Models\User;

$user = User::findOneByEmail('john@example.com');

Builder methods ​

The built-in models use a dedicated builder with extra methods:

BuilderMethodDescription
PostBuilderwhereTypes(...$types)Filter on one or several post types
whereStatus(...$status)Filter on one or several statuses (string or PostStatus)
whereAuthor($author)Filter on the author ID
findAllByType($type)Return all the posts of a post type
UserBuilderwhereEmail($email)Filter on the email
whereEmails(...$emails)Filter on several emails
whereLogin($login)Filter on the login
whereLogins(...$logins)Filter on several logins
CommentBuilderwhereTypes(...$types)Filter on one or several comment types
findAllByType($type)Return all the comments of a type
OptionBuilderwhereName($name)Filter on the option name
TermBuilderfindAllByTaxonomy($taxonomy)Return all the terms of a taxonomy

The methods with several values accept a list of arguments or an array:

php
use Dbout\WpOrm\Enums\PostStatus;
use Dbout\WpOrm\Models\Post;

$posts = Post::query()
    ->whereTypes('post', 'page')
    ->whereStatus([PostStatus::Publish, PostStatus::Private])
    ->get();

The builders of the models with metas (PostBuilder, UserBuilder) also have meta filters, see Metas.

Taps ​

A tap is a reusable filter applied with the Eloquent tap method:

php
use Dbout\WpOrm\Enums\PostStatus;
use Dbout\WpOrm\Models\Post;
use Dbout\WpOrm\Taps\Post\IsAuthorTap;
use Dbout\WpOrm\Taps\Post\IsStatusTap;

$posts = Post::query()
    ->tap(new IsAuthorTap(1))
    ->tap(new IsStatusTap(PostStatus::Publish))
    ->get();
ModelTapArgument
PostDbout\WpOrm\Taps\Post\IsAuthorTapint or User
Dbout\WpOrm\Taps\Post\IsStatusTapstring or PostStatus
Dbout\WpOrm\Taps\Post\IsPostTypeTapstring
Dbout\WpOrm\Taps\Post\IsPingStatusTapstring or PingStatus
AttachmentDbout\WpOrm\Taps\Attachment\IsMimeTypeTapstring (e.g. image/png)
CommentDbout\WpOrm\Taps\Comment\IsApprovedTapbool, true by default
Dbout\WpOrm\Taps\Comment\IsCommentTypeTapstring
Dbout\WpOrm\Taps\Comment\IsUserTapint or User
OptionDbout\WpOrm\Taps\Option\IsAutoloadTapbool or YesNo, YesNo::Yes by default

A tap is a simple invokable class, you can write your own:

php
use Dbout\WpOrm\Builders\PostBuilder;
use Dbout\WpOrm\Models\Post;

readonly class IsStickyTap
{
    public function __invoke(PostBuilder $builder): void
    {
        $builder->whereIn(Post::POST_ID, get_option('sticky_posts', []));
    }
}

Query builder ​

For everything else, use the Eloquent query builder:

php
use Dbout\WpOrm\Models\Post;

$posts = Post::query()
    ->where(Post::PING_STATUS, 'closed')
    ->whereYear(Post::DATE, 2026)
    ->with('author')
    ->get();

See the Eloquent documentation to learn more.

NOTE

The built-in builders select table.* by default, so that the joins (e.g. the meta filters) do not override the columns of the model. Use addSelect() to add columns.