WordPress ORM is a small library that adds a basic ORM into WordPress, which is easily extendable and includes models for core WordPress models such as posts, pages, comments and users. It's designed to allow you to easily add new models other than custom post types, and not have to write a lot of manual SQL.
While you can install and activate it like a normal plugin, I'd recommend putting it in the /wp-content/mu-plugins
folder and adding a script called wp-orm.php
to load the main plugin file (only top level .php
files are loaded from mu-plugins
).
wp-orm.php
:
<?php require 'wp-orm/wp-orm.php';
use WordPress\ORM\Model\Page;
$pages = Page::query()
->limit(5)
->where('post_status', 'publish')
->sort_by('post_title')
->order('ASC')
->find();
use WordPress\ORM\Model\User;
$user = User::find_one_by('user_login', 'brandon');
echo $user->get_user_login();
print_r($user->to_array());
use WordPress\ORM\Model\Post;
$posts = Post::query()
->limit(15)
->offset(0)
->where_all(['post_status' => 'publish', 'post_type' => 'post'])
->where_like('post_title', '%Hello world%')
->sort_by('post_title')
->order('ASC')
->find();
use WordPress\ORM\Model\Post;
$post = Post::find_one(1204);
$post->set_post_title('What an amazing post!');
$post->save();
Users, posts, pages and comments all support meta data.
$post = Post::find_one(1337);
$post->get_metadata('_edit_lock');
$post->update_metadata('_edit_lock', '');
$post->delete_metadata('_edit_lock');
Meta data is saved immediately using WordPress meta data functions under the hood. Calling save()
is not needed.
<?php
namespace WordPress\ORM;
class Venue extends BaseModel
{
protected $id;
protected $venue_title;
protected $description;
protected $now_playing;
protected $location;
protected $avg_rating;
public static function get_primary_key()
{
return 'id';
}
public static function get_table()
{
return 'wp_venues';
}
public static function get_searchable_fields()
{
return ['venue_title', 'description', 'now_playing'];
}
}
You can now use this venue, persist it, and use the custom query DSL shown above to query it.
This is a static method that you must define in your models, and should return the table to persist data to.
This is a static method that you must define in your models, and should return an array of properties to search when doing a search query.
Return's the property used as a primary key. Defaults to id
.
Create a new model from an array of properties.
Find a single model with the specified property value.
Find a single model who's primary key is equal to the given ID.
Return a new WordPress\ORM\Query
object.
Return every single model in the database.
Return the model's primary key (the value, not the property name).
Return all of the model's properties as an array.
Call right before save()
, should flatten any objects in the properties into strings so they can be persisted. Defaults to flattening DateTime
objects into a timestamp and arrays into a serialized array.
Save your model to the database. Creates a new row if the model doesn't have an ID, or updates an existing row if their is an ID.
Delete the model from the database. Returns true
if it was successful or false
if it was not.
Below are the functions you have access to after you call the Model::query()
function.
Limits the number of results returned using an SQL LIMIT
clause.
Offset the results returned, used with pagination. Uses the SQL OFFSET
clause.
Sort results by the specified property. Can also be a MySQL function such as RAND()
.
Order the results in the given order. Can be one of ASC
or DESC
.
Limit results to items matching the given search term. Searches the properties returned by Model::get_searchable_fields
.
Add a parameter to the where clause. Equivalent to WHERE $property = '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property != '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property LIKE '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property NOT LIKE '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property < '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property <= '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property > '$value'
. $value
is automatically escaped.
Add a parameter to the where clause. Equivalent to WHERE $property >= '$value'
. $value
is automatically escaped.
Limit results to items where the given column is one of the given values. Equivalent to WHERE $property IN ('value1', 'value2')
where value1
and value2
are values in the array.
Limit results to items where the given column is not one of the given values. Equivalent to WHERE $property NOT IN ('value1', 'value2')
where value1
and value2
are values in the array.
Limit results to items that match any of the property/value pairs given in the array. Must match at least one.
Limit results to items that match all of the property/value pairs given in the array.
Manipulate the raw SQL query created by the Query class.
add_filter('wporm_query', function($sql, $model_class) {
if ($model_class == 'WordPress\ORM\Model\Page') {
$sql = str_replace('wp_posts', 'wp2_posts', $sql);
}
return $sql;
}, 10, 2);
Manipulate the raw SQL query created by the Query class (the row count variation).
add_filter('wporm_count_query', function($sql, $model_class) {
if ($model_class == 'WordPress\ORM\Model\Page') {
$sql = str_replace('wp_posts', 'wp2_posts', $sql);
}
return $sql;
}, 10, 2);
This code is licensed under the MIT license.