Eloquent models for WordPress
WPEloquent uses Laravel's Eloquent ORM to interact with WordPress' database.
WPEloquent comes with the following Models:
- Post
- Page
- Attachment
- PostMeta
- Term
- TermMeta
- User
- UserMeta
- Comment
- CommentMeta
- Option
- Taxonomy
- TermRelationship
Install the brw/wp-eloquent package
composer require brw/wp-eloquent
functions.php
<?php
use WPEloquent\Database;
require __DIR__ . '/vendor/autoload.php';
// Basic usage
Database::connect();
// Advanced usage
Database::connect(
[
'driver' => 'mysql',
'prefix' => 'wp_',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'port' => '3306',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]
);
Each model has relationships defined to interact with other models. As an example, the Post model has terms, author, comments and so on.
Full documentation on the Fluent API: Eloquent ORM.
<?php
use WPEloquent\Models\Post;
$posts = Post::limit(10)->where('post_status', 'publish')->get();
foreach ($posts as $post) : ?>
<h2><?php echo $post->title; ?></h2>
<p><?php echo $post->author->email; ?></p>
<?php endforeach;
You can easily extend Models for Custom Post Types, or implement new Models for custom tables.
Define a new Model. For a custom post type, it's easiest to extend the
WPEloquent Post model, and change the $postType
property.
<?php
use WPEloquent\Models\Post;
class CustomPostType extends Post
{
/**
* The post type for the model.
*
* @var array|string
*/
$postType = 'custom_post_type';
}
To define a Model for a custom table, extend the WPEloquent Model and set the
$table
property to the name of the table. This will be prefixed automatically.
<?php
use WPEloquent\Models\Model;
class CustomTable extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'custom_table';
}
- DB facade
- Update database connection to use
$wpdb
rather than creating a new connection. - Improve database class to connect with a nicer API
- Add more documentation on Model relationships