Models are reusable PHP classes that encapsulate different data objects.
Used for WordPress custom-post-types, DTO objects, and other structures that are used by multiple classes.
When creating a model we can extend existing models that are built in our theme. Doing so will add core functionality specific to the model type.
We have extendable models for
gotoAndPlay/Models/Page.php
is the page post type model.gotoAndPlay/Models/Post.php
is the custom post type model.gotoAndPlay/Models/Term.php
is the taxonomy term model.gotoAndPlay/Models/User.php
is the user term model.Read more about creating custom post types in WordPress
Here is an example of a custom post type player
model with a name
custom field.
<?php
namespace gotoAndPlay\Models;
use gotoAndCore\Fields\Text;
class Player extends Post {
protected static $type = 'player';
protected function getView() {
return '@view-player';
}
public static function getPostTypeArgs() {
return [
'public' => true,
'hierarchical' => true,
'has_archive' => true,
'label' => __('Players', 'gotoandplay'),
'supports' => ['title', 'editor', 'thumbnail'],
];
}
public static function getFields() {
return [
'name' => new Text(),
];
}
}
When creating a custom post type model we also have to add the information to the theme. This is optional and required only if we want our theme to create the post type.
This can be done in gotoAndPlay/Theme.php
inside constructor when adding a custom post type to the array.
$this->postTypes = [
'attachment' => Image::class,
'post' => Article::class,
'player' => Player::class,
];
Don’t forget to use the class.
use gotoAndPlay\Models\Player;