WordPress ORM with Eloquent 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, post metas, users, comments and more.
The ORM is based on Eloquent ORM and uses the WordPress connection (wpdb class).
📖 Documentation: dimitribouteille.github.io/wp-orm
Tip
To simplify the integration of this library, we recommend using WordPress with one of the following tools: Bedrock, Themosis or Wordplate.
use Dbout\WpOrm\Enums\PostStatus;
use Dbout\WpOrm\Models\CustomPost;
// A model for your custom post type
class Product extends CustomPost
{
protected string $_type = 'product';
protected function metaCasts(): array
{
return ['price' => 'float'];
}
}
// Query it with Eloquent, filter on metas, eager load the relations
$products = Product::query()
->whereStatus(PostStatus::Publish)
->addMetaToFilter('featured', 'yes')
->with('author')
->orderBy(Product::DATE, 'desc')
->limit(10)
->get();
foreach ($products as $product) {
echo $product->getPostTitle(); // post_title column
echo $product->getMetaValue('price'); // float, thanks to the meta cast
echo $product->author->getDisplayName();
}
// Create or update, without SQL
$product = Product::find(42);
$product->setPostTitle('Blue T-shirt');
$product->save();
$product->setMeta('price', 19.90);No configuration is needed: the library uses the WordPress database connection.
- ✅ Support core WordPress models:
Comment,Option,Post,Term,TermTaxonomy,TermRelationship,User,PostMetaandUserMeta - ✅ Support core WordPress post types:
Article,AttachmentandPage - ✅ Based on core WordPress database connection (
wpdbclass), no configuration required - ✅ Custom functions to filter models with meta
- ✅ Meta casting (e.g. Attribute Casting)
- ✅ Database migrations with WP-CLI (
wp orm migrate) - ❤️ Easy integration of a custom post and comment type
- ❤️ Easy model creation for projects with custom tables
- ❤️ All the features available in Eloquent are usable with this library
Not yet developed but planned in a future version:
- 🗓️ Multisite support (network-shared tables and
switch_blog()handling)
Requires PHP >= 8.3 and WordPress >= 6.3.
composer require dbout/wp-ormMake sure the Composer autoloader is loaded (e.g. in your wp-config.php), and you are ready to go. See Installation for the details.
The full documentation is available on dimitribouteille.github.io/wp-orm. It only covers the specific points of this library, everything else works like Eloquent: see the documentation of Eloquent.
- Getting started: Installation, Quick start
- Models: Built-in models, Querying models, Metas, Custom models, Events
- Database: Database connection, Migrations
- Advanced: Security, Limitations
Warning
Every model is mass-assignable by default ($guarded = []) and the models bypass the WordPress hooks and object cache. Read Security and Limitations before using the library with user input.
🐞 This project includes two types of tests, both running on PHPUnit 12:
- Unit tests - Isolated tests without WordPress dependencies
- WordPress tests - Integration tests with WordPress core (uses
wp-phpunit/wp-phpunit)
# Unit tests
composer run test:unit
# WordPress tests (requires Docker)
./run-wp-tests.sh
# With coverage
./run-wp-tests.sh --coverageSee TESTING.md for detailed setup instructions and troubleshooting.
💕 🦄 We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements. Have a look at our contributing guidelines to find out how to raise a pull request.