vincentts/wp-models

Wrapper models around WP_Query

0.2 2023-05-25 11:25 UTC

This package is auto-updated.

Last update: 2025-06-25 18:06:02 UTC


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_field will be used if it exists. Otherwise it will use get_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.