Model Layer - 3ev/wordpress-core GitHub Wiki

Wordpress Core includes a number of model classes for common Wordpress entities, like Posts, Pages, Taxonomies, Terms and Authors. These models provide a uniform, object-oriented API to Wordpress data, rather than then confusing procedural functions that are part of the standard Wordpress API.

Instantiation of the model classes is made simple using factories. There are factory classes for each model, which provide methods for creating model instances from low-level Wordpress data.

###Instantiation

Most of the time, you'll be instantiating Post objects within The Loop. To make this as familiar and straightforward as possible, Wordpress Core provides the tev_post_factory() function.

A typical example:

<?php while (have_posts()): $p = tev_post_factory(); ?>

    <h1><?php echo $p->getTitle(); ?></h1>

<?php endwhile; ?>

Here, $p is an instance of Tev\Post\Model\Post. You can see how tev_post_factory() replaces the the_post() function.

The tev_post_factory() function is actually a simple wrapper around the Tev\Post\Factory::create() method. If you want to instantiate Post models in your own classes, you should inject an instance of the Tev\Post\Factory class and call the create() method directly.

For example:

<?php
namespace My\Plugin;

use Tev\Post\Factory as PostFactory;

class MyClass
{
    private $postFactory;

    public function __construct(PostFactory $postFactory)
    {
        $this->postFactory = $postFactory;
    }
    
    public function myGetPost()
    {
        // Calling create() with no args will get the current post from The Loop

        return $this->postFactory->create();
    }
}

####Instantiating other models using factories

Although you won't be using them most of the time, there are factories for all of the other model classes. You'll find them under the Tev\EntityType\Factory namespaces, where EntityType is something like Author.

###Built in models

####Post

The Post model provides a number of useful methods for fetching post data, including related taxonomies, author and meta data.

####Page

The Page model provides identical functionality to the Post model.

####Attachment

The Attachment model extends the Post model with extra methods to fetch data for the attached file.

####Author

The Author model provides methods for fetching data on Post authors.

####Taxonomy

The Taxonomy model provides methods for fetching base taxonomy data, as well as all of the Terms within that taxonomy.

####Term

The Term model provides methods for fetching individual Term data.

###Overriding the built-in model classes

tev_post_factory() will automatically decide which model class to return based on the Post Type (post, page, attachment etc).

If you'd like, you can modify the model class that's returned by registering a new post type model using one of the built in model keys. However, this is not recommended as it may cause your code to conflict with that of other plugins.

⚠️ **GitHub.com Fallback** ⚠️