vincentts / wp-models
Wrapper models around WP_Query
Installs: 42
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/vincentts/wp-models
Requires
- php: ^8.0
README
Wrapper class around WP Query to create models.
Installations
Install the package through composer by running composer require "vincentts/wp-models".
Getting Started
Define model classes by extending PostModel or TermModel.
Post Model
namespace Models; use Vincentts\WpModels\PostModel; class Post extends PostModel { protected $post_type = 'post'; }
Term Model
namespace Models; use Vincentts\WpModels\TermModel; class Category extends TermModel { protected $taxonomy = 'category'; }
Defining relationships
namespace Models; use Vincentts\WpModels\PostModel; use Models\Category; class Post extends PostModel { protected $post_type = 'post'; public function categories() { return $this->has( Category::class ); } }
Usage
Getting posts with relationships
use Models\Post; $posts = (new Post())->with(['categories'])->get();
Including meta data
Note ACF
get_fieldwill be used if it exists. Otherwise it will useget_post_meta.
use Models\Post; $posts = (new Post())->meta(['summary', 'description'])->with(['categories'])->get();
There are also other methods similar to the arguments from WP_Query.