PHPackages                             wenprise/eloquent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. wenprise/eloquent

ActiveLibrary[Database &amp; ORM](/categories/database)

wenprise/eloquent
=================

Eloquent ORM for WordPress

9.1.1(1y ago)464611MITPHP

Since Jan 31Pushed 1y ago3 watchersCompare

[ Source](https://github.com/iwillhappy1314/wenprise-eloquent)[ Packagist](https://packagist.org/packages/wenprise/eloquent)[ Docs](https://github.com/iwillhappy1314/wenprise-eloquent)[ RSS](/packages/wenprise-eloquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (11)Used By (1)

Credits
=======

[](#credits)

Thanks to:

1.
2.

Eloquent Wrapper for WordPress
==============================

[](#eloquent-wrapper-for-wordpress)

This is a library package to use Laravel's [Eloquent ORM](http://laravel.com/docs/5.5/eloquent) with WordPress.

How it Works
------------

[](#how-it-works)

- Eloquent is mainly used here as the query builder
- [WPDB](http://codex.wordpress.org/Class_Reference/wpdb) is used to run queries built by Eloquent
- Hence, we have the benfit to use plugins like `debug-bar` or `query-monitor` to get SQL query reporting.
- It doesn't create any extra MySQL connection

Minimum Requirement
-------------------

[](#minimum-requirement)

- PHP 7.0
- WordPress 3.6+

Package Installation
--------------------

[](#package-installation)

`$ composer require wenprise/eloquent`

Usage Example
=============

[](#usage-example)

Basic Usage
-----------

[](#basic-usage)

```
$db = \Wenprise\Eloquent\Connection::instance();

var_dump( $db->table('users')->find(1) );
var_dump( $db->select('SELECT * FROM wp_users WHERE id = ?', [1]) );
var_dump( $db->table('users')->where('user_login', 'john')->first() );

// OR with DB facade
use \Wenprise\Eloquent\Facades\DB;

var_dump( DB::table('users')->find(1) );
var_dump( DB::select('SELECT * FROM wp_users WHERE id = ?', [1]) );
var_dump( DB::table('users')->where('user_login', 'john')->first() );
```

Posts
-----

[](#posts)

```
use Wenprise\ORM\WP\Post;

// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// A specific post
$post = Post::find(31);
echo $post->post_title;
```

Pages
-----

[](#pages)

Pages are like custom post types. You can use `Post::type('page')` or the `Wenprise\ORM\WP\Page` class.

```
// Find a page by slug
$page = Page::slug('about')->first(); // OR
$page = Post::type('page')->slug('about')->first();
echo $page->post_title;
```

Comments
--------

[](#comments)

```
use Wenprise\ORM\WP\Comment;

// Get Comment with id 12345
$comment = Comment::find(12345);

// Get related data
$comment->post;
$comment->author;
$comment->meta
```

Meta Data (Custom Fields)
-------------------------

[](#meta-data-custom-fields)

You can retrieve meta data from posts too.

```
// Get a custom meta value (like 'link' or whatever) from a post (any type)
$post = Post::find(31);
echo $post->meta->link; // OR
echo $post->fields->link;
echo $post->link; // OR
```

To create or update meta data form a User just use the `saveMeta()` or `saveField()` methods. They return `bool` like the Eloquent `save()` method.

```
$post = Post::find(1);
$post->saveMeta('username', 'jgrossi');
```

You can save many meta data at the same time too:

```
$post = Post::find(1);
$post->saveMeta([
    'username' => 'jgrossi',
    'url' => 'http://jgrossi.com',
]);
```

You also have the `createMeta()` and `createField()` methods, that work like the `saveX()` methods, but they are used only for creation and return the `PostMeta` created instance, instead of `bool`.

```
$post = Post::find(1);
$postMeta = $post->createMeta('foo', 'bar'); // instance of PostMeta class
$trueOrFalse = $post->saveMeta('foo', 'baz'); // boolean
```

### Querying Posts by Custom Fields (Meta)

[](#querying-posts-by-custom-fields-meta)

There are multiples possibilities to query posts by their custom fields (meta). Just use the `hasMeta()` scope under `Post` (actually for all models using the `HasMetaFields` trait) class:

```
// Using just one custom field
$post = Post::published()->hasMeta('username', 'jgrossi')->first(); // setting key and value
$post = Post::published()->hasMeta('username'); // setting just the key
```

You can also use the `hasMeta()` scope passing an array as parameter:

```
$post = Post::hasMeta(['username' => 'jgrossi'])->first();
$post = Post::hasMeta(['username' => 'jgrossi', 'url' => 'jgrossi.com'])->first();
// Or just passing the keys
$post = Post::hasMeta(['username', 'url'])->first();
```

### Fields Aliases

[](#fields-aliases)

The `Post` class has support to "aliases", so if you check the `Post` class you should note some aliases defined in the static `$aliases` array, like `title` for `post_title` and `content` for `post_content`.

```
$post = Post::find(1);
$post->title === $post->post_title; // true
```

If you're extending the `Post` class to create your own class you can use `$aliases` too. Just add new aliases to that static property inside your own class and it will automatically inherit all aliases from parent `Post` class:

```
class A extends Post
{
    protected static $aliases = [
        'foo' => 'post_foo',
    ];
}

$a = A::find(1);
echo $a->foo;
echo $a->title; // from Post class
```

### Custom Scopes

[](#custom-scopes)

To order posts you can use `newest()` and `oldest()` scopes, for both `Post` and `User` classes:

```
$newest = Post::newest()->first();
$oldest = Post::oldest()->first();
```

### Pagination

[](#pagination)

To order posts just use Eloquent `paginate()` method:

```
$posts = Post::published()->paginate(5);
foreach ($posts as $post) {
    // ...
}
```

To display the pagination links just call the `links()` method:

```
{{ $posts->links() }}
```

Post Taxonomies
---------------

[](#post-taxonomies)

You can get taxonomies for a specific post like:

```
$post = Post::find(1);
$taxonomy = $post->taxonomies()->first();
echo $taxonomy->taxonomy;
```

Or you can search for posts using its taxonomies:

```
$post = Post::taxonomy('category', 'php')->first();
```

Categories and Taxonomies
-------------------------

[](#categories-and-taxonomies)

Get a category or taxonomy or load posts from a certain category. There are multiple ways to achieve it.

```
// all categories
$cat = Taxonomy::category()->slug('uncategorized')->first()->posts();
echo ""; print_r($cat->name); echo "";

// only all categories and posts connected with it
$cat = Taxonomy::where('taxonomy', 'category')->with('posts')->get();
$cat->each(function($category) {
    echo $category->name;
});
```

Attachment and Revision
-----------------------

[](#attachment-and-revision)

Getting the attachment and/or revision from a `Post` or `Page`.

```
$page = Page::slug('about')->with('attachment')->first();
// get feature image from page or post
print_r($page->attachment);

$post = Post::slug('test')->with('revision')->first();
// get all revisions from a post or page
print_r($post->revision);
```

Users
-----

[](#users)

You can manipulate users in the same manner you work with posts:

```
// All users
$users = User::get();

// A specific user
$user = User::find(1);
echo $user->user_login;
```

Options
-------

[](#options)

You can use the `Option` class to get data from `wp_options` table:

```
$siteUrl = Option::get('siteurl');
```

You can also add new options:

```
Option::add('foo', 'bar'); // stored as string
Option::add('baz', ['one' => 'two']); // this will be serialized and saved
```

You can get all options in a simple array:

```
$options = Option::asArray();
echo $options['siteurl'];
```

Or you can specify only the keys you want to get:

```
$options = Option::asArray(['siteurl', 'home', 'blogname']);
echo $options['home'];
```

Writing a Model
---------------

[](#writing-a-model)

```
use \Wenprise\Eloquent\Model;

class Employee extends Model {

    /**
    * Name for table without prefix, the model can automatic add it
    *
    * @var string
    */
    protected $table = 'table_name';

    /**
    * Columns that can be edited
    *
    * @var array
    */
    protected $fillable = [];

    /**
    * Disable created_at and update_at columns, unless you have those.
    */
	 public $timestamps = false;

    /**
    * Set primary key as ID, because WordPress
    *
    * @var string
    */
	 protected $primaryKey = 'ID';

    /**
     * Make ID guarded -- without this ID doesn't save.
     *
     * @var string
     */
    protected $guarded = [ 'ID' ];

    /**
     * The column names allow to be filled
     * @var array
     */
    protected $fillable = [
        'name',
        'status',
    ];

}

var_dump( Employee::all()->toArray() ); // gets all employees
var_dump( Employee::find(1) ); // find employee with ID 1
```

The class name `Employee` will be translated into `PREFIX_employees` table to run queries. But as usual, you can override the table name.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~269 days

Recently: every ~101 days

Total

10

Last Release

602d ago

Major Versions

0.1.2 → 5.5.02023-08-12

5.5.0 → 8.0.02023-08-12

8.8.1 → 9.0.02023-11-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/a0fa49958b7c45c0b373b185feb5debe5bdd35be3b3536bc44477d3bd3a2d435?d=identicon)[iwillhappy1314](/maintainers/iwillhappy1314)

---

Top Contributors

[![iwillhappy1314](https://avatars.githubusercontent.com/u/1455683?v=4)](https://github.com/iwillhappy1314 "iwillhappy1314 (31 commits)")

---

Tags

pluginwordpressormsqleloquent

### Embed Badge

![Health badge](/badges/wenprise-eloquent/health.svg)

```
[![Health](https://phpackages.com/badges/wenprise-eloquent/health.svg)](https://phpackages.com/packages/wenprise-eloquent)
```

###  Alternatives

[tareq1988/wp-eloquent

Eloquent ORM for WordPress

57254.7k](/packages/tareq1988-wp-eloquent)[dbout/wp-orm

WordPress ORM with Eloquent.

1279.6k1](/packages/dbout-wp-orm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
