quentingab / wordpress-orm
WordPress ORM
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: >=8.0
- squizlabs/php_codesniffer: ^3.0
README
Wordpress ORM provide an easy way to interact with WordPress database, query, insert and update posts. The syntax is heavily inspired by Eloquent ORM and Laravel.
The package provide two kind of models: Model
to interact with custom table in WordPress and Wodel
to interact with WP_Query in a more natural way ( and it also works well with ACF).
This package only use WP builtin function like WP_Query
, wp_insert_post
and wpdb
.
Install
Via Composer
$ composer require quentingab/wordpress-orm
Usage with WordPress posts
Get all posts/page or custom post type
$posts = \QuentinGab\WordpressOrm\Wodel::init()->type('post')->all(); foreach($posts as $post){ echo $post->post_title; }
Get current post with acf
$post = \QuentinGab\WordpressOrm\Wodel::init()->current();
Update a post
$post = \QuentinGab\WordpressOrm\Wodel::init()->current(); $post->post_title = "Hello World"; $post->save();
Insert a post
$post = new \QuentinGab\WordpressOrm\Wodel( [ 'post_title'=>'Hello World', 'acf' => [ 'field_name' => 'value' ] ] ); $post->save();
Extend the wordpress-orm
class Page extends \QuentinGab\WordpressOrm\Wodel { protected $post_type = 'page'; //only necessary if you want to insert a new post programmatically //otherwise the acf fields will not be populated //If you only get Wodel or update existing Wodel you can omit $acf_keys protected $acf_keys = [ 'the_field_name' => 'the_field_key', 'color' => 'field_5f7848684c404', ]; } $page = Page::init()->find(1); echo $page->acf['color'];
Usage with custom table
if you have data stored in a custom table you can use \QuentinGab\WordpressOrm\Model to interact with the database. Under the hood it only use default WordPress object $wpdb.
Example of a custom table
global $wpdb; $table_name = 'events'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, title varchar(255), active boolean DEFAULT 0 NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; dbDelta($sql);
Create a Model class
class Event extends \QuentinGab\WordpressOrm\Model { protected $table = 'events'; protected $primary_key = "id"; protected $fillable = [ 'title' ]; protected $casts = [ 'active' => 'bool', 'created_at' => "date" ]; }
Get Model
$all = Event::init()->all(); $only_active = Event::init()->where(['active'=>true])->get(); $with_primary_key_1 = Event::init()->find(1);
Save Model
$new_event = new Event(['title'=>'my new event','active'=>false]); $new_event->save();
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email quentin.gabriele@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.