PHPackages                             maksimer/wp-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. maksimer/wp-eloquent

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

maksimer/wp-eloquent
====================

Eloquent ORM for WordPress

1.0.0(6y ago)04.5k↓100%GPL-2.0-or-laterPHP

Since Dec 16Pushed 6y agoCompare

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

READMEChangelogDependencies (2)Versions (2)Used By (0)

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

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

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

This package is based on the work from .

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

[](#package-installation)

To install this package, edit your `composer.json` file:

`composer require maksimer/wp-eloquent`

Now run:

`$ composer install`

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

[](#usage-example)

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

[](#basic-usage)

```
$db = \Maksimer\ORM\Eloquent\Database::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 \Maksimer\ORM\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() );
```

Creating Models For Custom Tables
---------------------------------

[](#creating-models-for-custom-tables)

You can use custom tables of the WordPress databases to create models:

```
	namespace whatever;

	class CustomTableModel extends \Maksimer\ORM\Eloquent\Model {

		/**
		 * Name for table without prefix
		 *
		 * @var string
		 */
		protected $table = 'table_name';

		/**
		 * Columns that can be edited - IE not primary key or timestamps if being used
		 */
		protected $fillable = [
			'city',
			'state',
			'country'
		];

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

		/** Everything below this is best done in an abstract class that custom tables extend */

		/**
		 * 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' ];

		/**
		 * Overide parent method to make sure prefixing is correct.
		 *
		 * @return string
		 */
		public function getTable()
		{
			//In this example, it's set, but this is better in an abstract class
			if( isset( $this->table ) ){
				$prefix =  $this->getConnection()->db->prefix;
				return $prefix . $this->table;

			}

			return parent::getTable();
		}

	}

```

### Retrieving All Rows From A Table

[](#retrieving-all-rows-from-a-table)

```
$users = $db->table('users')->get();

foreach ($users as $user) {
    var_dump($user->display_name);
}
```

Here `users` is the table name **without prefix**. The prefix will be applied automatically.

### Other Examples

[](#other-examples)

- [Queries](http://laravel.com/docs/5.0/queries)
- [Eloquent ORM](http://laravel.com/docs/5.0/eloquent)

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

[](#writing-a-model)

```
use \Maksimer\ORM\Eloquent\Model as Model;

class Employee extends Model {

}

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.

### In-built Models for WordPress

[](#in-built-models-for-wordpress)

- Post
- Comment
- Post Meta
- User
- User Meta

```
use Maksimer\ORM\WP\Post as Post;

var_dump( Post::all() ); //returns only posts with WordPress post_type "post"
```

#### Filter `Post` by `post_status` and `post_type`

[](#filter-post-by-post_status-and-post_type)

```
use Maksimer\ORM\WP\Post as Post;
var_dump(Post::type('page')->get()->toArray()); // get pages
var_dump(Post::status('publish')->get()->toArray()); // get posts with publish status
var_dump(Post::type('page')->status('publish')->get()->toArray()); // get pages with publish status
```

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 5.6.4+
- WordPress 4.0+

Author
------

[](#author)

[Tareq Hasan](https://tareq.co)

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 64.8% 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

Unknown

Total

1

Last Release

2336d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2cf1fa8c2312040da99aef77d876e0c2514fb4b9f1ed0f0235338df54028b5e9?d=identicon)[maksimer](/maintainers/maksimer)

---

Top Contributors

[![tareq1988](https://avatars.githubusercontent.com/u/153669?v=4)](https://github.com/tareq1988 "tareq1988 (35 commits)")[![sohelamin](https://avatars.githubusercontent.com/u/1708683?v=4)](https://github.com/sohelamin "sohelamin (5 commits)")[![dsge](https://avatars.githubusercontent.com/u/5737250?v=4)](https://github.com/dsge "dsge (3 commits)")[![monkeymonk](https://avatars.githubusercontent.com/u/574338?v=4)](https://github.com/monkeymonk "monkeymonk (2 commits)")[![sabbir1991](https://avatars.githubusercontent.com/u/2692939?v=4)](https://github.com/sabbir1991 "sabbir1991 (1 commits)")[![Shelob9](https://avatars.githubusercontent.com/u/1994311?v=4)](https://github.com/Shelob9 "Shelob9 (1 commits)")[![sultann](https://avatars.githubusercontent.com/u/5239470?v=4)](https://github.com/sultann "sultann (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")[![asaquzzaman](https://avatars.githubusercontent.com/u/1366288?v=4)](https://github.com/asaquzzaman "asaquzzaman (1 commits)")[![YevhenMikulenko](https://avatars.githubusercontent.com/u/5243125?v=4)](https://github.com/YevhenMikulenko "YevhenMikulenko (1 commits)")[![JoeCrescoll](https://avatars.githubusercontent.com/u/14929732?v=4)](https://github.com/JoeCrescoll "JoeCrescoll (1 commits)")[![mirzazeyrek](https://avatars.githubusercontent.com/u/6233650?v=4)](https://github.com/mirzazeyrek "mirzazeyrek (1 commits)")[![polevaultweb](https://avatars.githubusercontent.com/u/1770201?v=4)](https://github.com/polevaultweb "polevaultweb (1 commits)")

---

Tags

pluginwordpressormsqleloquent

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/maksimer-wp-eloquent/health.svg)](https://phpackages.com/packages/maksimer-wp-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)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

450191.6k1](/packages/mehdi-fathi-eloquent-filter)

PHPackages © 2026

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