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

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

cmachu/wp-eloquent-models
=========================

A Laravel wrapper for wordpress which turns all Wordpress models into Laravel Eloquent Models.

0.4.5(2y ago)0342MITPHPPHP ^8.1

Since Jan 4Pushed 2y agoCompare

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

READMEChangelog (6)Dependencies (3)Versions (7)Used By (0)

Wordpress Laravel Eloquent Models PHP &gt;= 8.1
===============================================

[](#wordpress-laravel-eloquent-models-php--81)

[![](https://camo.githubusercontent.com/1dfedbae65fc9038b1823809b2675564207682c6d22b4e53d1c8ad77e4cbfbde/687474703a2f2f647265776a626172746c6574742e636f6d2f696d616765732f6769746875622f6c6f676f2d6c61726176656c2e737667)](https://camo.githubusercontent.com/1dfedbae65fc9038b1823809b2675564207682c6d22b4e53d1c8ad77e4cbfbde/687474703a2f2f647265776a626172746c6574742e636f6d2f696d616765732f6769746875622f6c6f676f2d6c61726176656c2e737667)[![](https://camo.githubusercontent.com/7aa74a43df31c0c0df3a4e7346efbbd7fc597033950a3a82ce3752b0feb82465/687474703a2f2f647265776a626172746c6574742e636f6d2f696d616765732f6769746875622f776f726470726573732d6c6f676f2e706e67)](https://camo.githubusercontent.com/7aa74a43df31c0c0df3a4e7346efbbd7fc597033950a3a82ce3752b0feb82465/687474703a2f2f647265776a626172746c6574742e636f6d2f696d616765732f6769746875622f776f726470726573732d6c6f676f2e706e67)

A forked library for PHP &gt;= 8.1 that converts converts wordpress tables into [Laravel Eloquent Models](https://laravel.com/docs/5.4/eloquent). This is can be helpful for any wordpress project where maybe you'd rather use the awesome features of Laravel's Eloquent.

Or maybe you're building a project using Laravel and Roots Bedrock/Sage/etc and want to access WordPress data within Laravel. Or maybe you're writing an API with something like [Slim](https://www.slimframework.com/) or better yet [Lumen](https://lumen.laravel.com/) don't want to increase your load time by loading the entire WP core.

This is a great boiler plate based off [Eloquent](https://laravel.com/docs/5.4/eloquent) by Laravel to get you going.

*Note:* This is documentation for additional functionality on top of Eloquent. For documentation on all of Eloquent's features you visit the [documentation](https://laravel.com/docs/5.4/eloquent).

Overview
========

[](#overview)

- [Installation](#installing-wpeloquent)
- [Setup](#setup---common)
- [Setup with Laravel](#setup-with-laravel)
- [Posts](#posts)
- [Comments](#comments)
- [Terms](#terms)
- [Users](#users)
- [Meta](#meta)
- [Options](#options)
- [Links](#links)
- [Extending your own models](#extending-your-own-models)
- [Query Logs](#query-logs)

Installing WPEloquent
---------------------

[](#installing-wpeloquent)

The recommended way to install WPEloquent is through [Composer](https://getcomposer.org/).

```
composer require mallardduck/wp-eloquent-models
```

Version Guidance
----------------

[](#version-guidance)

Current version: 0.4.2

Setup - Common
--------------

[](#setup---common)

```
require_once('vendor/autoload.php');

\WPEloquent\Core\Laravel::connect([
    'global' => true,

    'config' => [

        'database' => [
            'user'     => 'user',
            'password' => 'password',
            'name'     => 'database',
            'host'     => '127.0.0.1',
            'port'     => '3306'
        ],

        // your wpdb prefix
        'prefix' => 'wp_',
    ],

    // enable events
    'events' => false,

    // enable query log
    'log'    => true
]);
```

If you wanted to enable this on your entire WP install you could create a file with the above code to drop in the `mu-plugins` folder.

Setup with Laravel
------------------

[](#setup-with-laravel)

These directions are for when you want to work with WordPress database using Eloqent inside Laravel. The easiest mehtod is making a new config inside Laravel's `config/database.php`. For example:

```
    'wordpress' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_NAME', 'wordpress'),
        'username' => env('DB_USER', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => env('DB_PREFIX', 'wp_'),
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
```

*Note:* In this example the WordPress database and Laravel database can be accessed on the same server by the same MySQL user. The laravel `.env` of your project may need to be adjusted to match this example.

Then in your `AppServiceProvider` add the following line:

```
BaseModel::setDefaultConnection('wordpress');
```

Supported Models
----------------

[](#supported-models)

### Posts

[](#posts)

```
use \WPEloquent\Model\Post;

// getting a post
$post = Post::find(1);

// available relationships
$post->author;
$post->comments;
$post->terms;
$post->tags;
$post->categories;
$post->meta;
```

***Statuses***

By default, the `Post` returns posts with all statuses. You can however override this with the [local scope](https://laravel.com/docs/5.3/eloquent#query-scopes) `published` to return only published posts.

```
Post::published()->get();
```

Or if you need a specific status you can override with defined status via [local scope](https://laravel.com/docs/5.3/eloquent#query-scopes).

```
Post::status('draft')->get();
```

***Post Types***

By default, the `Post` returns posts with all post types. You can however override this by defining a post type via [local scope](https://laravel.com/docs/5.3/eloquent#query-scopes).

```
Post::type('page')->get();
```

### Comments

[](#comments)

```
use \WPEloquent\Model\Comment;

// getting a comment
$comment = Comment::find(12345);

// available relationships
$comment->post;
$comment->author;
$comment->meta
```

### Terms

[](#terms)

In this version `Term` is still accesible as a model but is only leveraged through posts.

```
$post->terms()->where('taxonomy', 'country');
```

### Users

[](#users)

```
use \WPEloquent\Model\User;

// getting a comment
$user = User::find(123);

// available relationships
$user->posts;
$user->meta;
$user->comments
```

### Meta

[](#meta)

The models `Post`, `User`, `Comment`, `Term`, all implement the `HasMeta`. Therefore they meta can easily be retrieved by the `getMeta` and set by the `setMeta` helper functions:

```
$post = Post::find(1);
$post->setMeta('featured_image', 'my-image.jpg');
$post->setMeta('breakfast', ['waffles' => 'blueberry', 'pancakes' => 'banana']);

// or all in one call
$featured_image = Post::find(1)->getMeta('featured_image');
Post::find(1)->setMeta('featured_image', 'image.jpg');

// same applies for all other models

$user = User::find(1)
$facebook = $user->getMeta('facebook');
$user->setMeta('social', ['facebook' => 'facebook.com/me', 'instagram' => 'instagram.com/me']);

$comment = Comment::find(1);
$meta = $comment->getMeta('some_comment_meta');

$term = Term::find(123);
$meta = $term->getMeta('some_term_meta');

// delete meta
$post = Post::find(123)->deleteMeta('some_term_meta');
```

### Options

[](#options)

In wordpress you can use `get_option`. Alternatively, if you don't want to load the wordpress core you can use helper function `getValue`.

```
use \WPEloquent\Model\Post;

$siteurl = Option::getValue('siteurl');
```

Or of course, the long form:

```
use \WPEloquent\Model\Options;

$siteurl = Option::where('option_name', 'siteurl')->value('option_value');
```

### Links

[](#links)

```
use \WPEloquent\Model\Link;

$siteurl = Link::find(1);
```

Extending your own models
-------------------------

[](#extending-your-own-models)

If you want to add your own functionality to a model, for instance a `User` you can do so like this:

```
namespace App\Model;

class User extends \WPEloquent\Model\User {

    public function orders() {
        return $this->hasMany('\App\Model\User\Orders');
    }

    public function current() {
        // some functionality to get current user
    }

    public function favorites() {
        return $this->hasMany('Favorites');
    }

}
```

Another example would be for custom taxonomies on a post, say `country`

```
namespace App\Model;

class Post extends \WPEloquent\Model\Post {

    public function countries() {
        return $this->terms()->where('taxonomy', 'country');
    }

}

Post::with(['categories', 'countries'])->find(1);
```

Or maybe you need to access a custom post type, like:

```
namespace App\Model;

class CustomPostType extends \WPEloquent\Model\Post {
    protected $post_type  = 'custom_post_type';

    public static function getBySlug(string $slug): self
    {
        return self::where('post_name', $slug)->firstOrfail();
    }
}

CustomPostType::with(['categories', 'countries'])->find(1);
```

Query Logs
----------

[](#query-logs)

Sometimes it's helpful to see the query logs for debugging. You can enable the logs by passing `log` is set to `true` (see [setup](#setup)) on the `Laravel::connect` method. Logs are retrieved by running.

```
use \WPEloquent\Core\Laravel;

print_r(Laravel::queryLog());
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 51.4% 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 ~49 days

Recently: every ~61 days

Total

6

Last Release

982d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2819629f4315c9a57c2c1d2525337a3b26adf4b3e9179d81bb800f2b47924fb9?d=identicon)[cmachu](/maintainers/cmachu)

---

Top Contributors

[![drewjbartlett](https://avatars.githubusercontent.com/u/2146829?v=4)](https://github.com/drewjbartlett "drewjbartlett (38 commits)")[![mallardduck](https://avatars.githubusercontent.com/u/619938?v=4)](https://github.com/mallardduck "mallardduck (26 commits)")[![terence1990](https://avatars.githubusercontent.com/u/8171301?v=4)](https://github.com/terence1990 "terence1990 (5 commits)")[![riozzi](https://avatars.githubusercontent.com/u/7408598?v=4)](https://github.com/riozzi "riozzi (4 commits)")[![twosg](https://avatars.githubusercontent.com/u/325412?v=4)](https://github.com/twosg "twosg (1 commits)")

---

Tags

laravelwordpresseloquent

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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