tombroucke / wp-models
Interact with posts, terms & users in a OOP way
Installs: 1 464
Dependents: 4
Suggesters: 1
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8
Requires (Dev)
- 10up/wp_mock: ^0.5.0
- php-stubs/acf-pro-stubs: ^6.0
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.6.2
- szepeviktor/phpstan-wordpress: ^1.1
README
composer require tombroucke/wp-models
Interacting with models
Create a new class for your custom post type
namespace Otomaties\Events\Models; use Otomaties\WpModels\PostType; class Event extends PostType { /** * This method will override the parent method which shows the post date. */ public function date() : DateTime { $date = $this->meta()->get('date'); return $date ? DateTime::createFromFormat('Ymd', $date) : null; } public function time() : string { return substr($this->meta()->get('time'), 0, 5); } // ... more custom methods public static function postType() : string { return 'event'; } }
Client Code
$event = new Otomaties\Events\Models\Event(420); esc_html_e($event->getId()); // Post ID esc_html_e($event->title()); // Post title esc_html_e($event->slug()); // Post slug esc_html_e($event->meta()->get('meta_key')); // Meta (single) esc_html_e($event->meta()->get('meta_keys', false)); // Meta (multiple) esc_html_e($event->date()->format('d-m-Y')); // Custom method esc_html_e($event->time()); // Custom method $event->meta()->set('meta_key', 'meta_value'); $event->meta()->add('meta_key', 'meta_value_2'); // See src/PostType.php for all default methods
Insert, update, delete & query models
Inserting
use Otomaties\WpModels\PostTypeRepository; $repository = new PostTypeRepository(Event::class); $args = [ 'post_title' => 'Event title', 'post_status' => 'draft', 'post_content' => 'Event content', 'meta_input' => [ 'key' => 'value' ] ] $event = $repository->insert($args); // returns instance of Event::class
or
$args = [ 'post_title' => 'Event title', 'post_status' => 'draft', 'post_content' => 'Event content', 'meta_input' => [ 'key' => 'value' ] ] $event = Event::insert($args);
Updating
use Otomaties\WpModels\PostTypeRepository; $event = new Event(420); $repository = new PostTypeRepository(Event::class); $args = [ 'post_title' => 'Event title', 'post_status' => 'draft', 'post_content' => 'Event content', 'meta_input' => [ 'key' => 'value' ] ] $event = $repository->update($event, $args); // returns instance of Event::class
or
$event = new Event(420); $args = [ 'post_title' => 'Event title', 'post_status' => 'draft', 'post_content' => 'Event content', 'meta_input' => [ 'key' => 'value' ] ] $event = Event::update($event, $args);
Deleting
use Otomaties\WpModels\PostTypeRepository; $event = new Event(420); $repository = new PostTypeRepository(Event::class); $event = $repository->delete($event);
or
$event = new Event(420); $event = Event::delete($event);
Querying
All posts
use Otomaties\WpModels\PostTypeRepository; $repository = new PostTypeRepository(Event::class); $allEvents = $repository->find(); // Returns post type collection $tenEventsOffsetTen = $repository->find(null, 10, 10); // Returns post type collection
or
$allEvents = Event::find(); // Returns post type collection
By id
use Otomaties\WpModels\PostTypeRepository; $repository = new PostTypeRepository(Event::class); $event = $repository->find(420)->first(); // Returns PostType object (Event object in this case)
or
$event = Event::find(420)->first(); // Returns PostType object (Event object in this case)
Custom query
use Otomaties\WpModels\PostTypeRepository; $repository = new PostTypeRepository(Event::class); $args = [ 'meta_query' => [ 'relation' => 'OR', [ 'key' => 'date', 'value' => date('Ymd'), 'compare' => '>=' ], [ 'key' => 'date', 'compare'=>'NOT EXISTS', ] ] ]; $event = $repository->find($args); // Returns Collection
or
$args = [ 'meta_query' => [ 'relation' => 'OR', [ 'key' => 'date', 'value' => date('Ymd'), 'compare' => '>=' ], [ 'key' => 'date', 'compare'=>'NOT EXISTS', ] ] ]; $event = Event::find($args); // Returns Collection