vincentts / wp-models
围绕 WP_Query 的包装模型
0.2
2023-05-25 11:25 UTC
Requires
- php: ^8.0
README
围绕 WP Query 创建模型的包装类。
安装
通过运行 composer require "vincentts/wp-models"
通过 composer 安装此包。
入门指南
通过扩展 PostModel
或 TermModel
定义模型类。
帖子模型
namespace Models; use Vincentts\WpModels\PostModel; class Post extends PostModel { protected $post_type = 'post'; }
术语模型
namespace Models; use Vincentts\WpModels\TermModel; class Category extends TermModel { protected $taxonomy = 'category'; }
定义关系
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 ); } }
用法
获取带关系的帖子
use Models\Post; $posts = (new Post())->with(['categories'])->get();
包含元数据
注意 如果存在,将使用 ACF
get_field
。否则将使用get_post_meta
。
use Models\Post; $posts = (new Post())->meta(['summary', 'description'])->with(['categories'])->get();
还有其他类似于 WP_Query
参数的方法。