-
Notifications
You must be signed in to change notification settings - Fork 1
Repositories
Wordpress Core provides a few built-in repository classes. These classes can be used to pull data from the database, and mostly use WP_Query under the hood.
###Built in repository classes
####Post Repository
The post repository class provides a couple of methods for retrieving posts and related data from the database.
####Taxonomy Repository
Currently the Taxonomy Repository class doesn't provide any functionality, but it will do in future.
####Term Repository
The term repository class provides a couple of methods for retrieving taxonomy terms from the database.
###Creating your own repositories
If you need to pull out any custom data from the Wordpress database, you should create your own repository class to house all of you custom queries. Keeping these queries inside a repository class rather the inside say, an action provider, means that query can be re-used throughout your codebase without duplicating code.
Repositories are just plain PHP classes, although you may want to extend from one of the built-in classes as a starting point. Once you've made your class, you should bind it into the DI container to make it accessible to your application.
For example, to create a repo for your own custom post type:
my-plugin/src/MyPluginVendor/Repository/CustomPostTypeRepository.php:
<?php
namespace MyPluginVendor\Repository;
use WP_Query;
use Tev\Post\Repository\PostRepository;
class CustomPostTypeRepository extends PostRepository
{
public function getAllCustomPosts()
{
$q = new WP_Query(array_merge(array(
'post_type' => 'mycustomposttype',
'nopaging' => true
), $queryVars));
$posts = array();
foreach ($q->get_posts() as $p) {
$posts[] = $this->postFactory->create($p);
}
return $posts;
}
}my-plugin/my-plugin.php:
<?php
/*
Plugin Name: My Plugin
Plugin URI: http://www.example.com/
Description: An example plugin.
Author: Me
Author URI: http://www.example.com/
Version: 1.0.0
*/
// Bind the type into the container, resolving its dependencies
tev_app()->bind('my_custom_post_repo', function ($app) {
return new MyPluginVendor\Repository\CustomPostTypeRepository($app->fetch('post_factory'));
});
tev_fetch('plugin_loader')->load(__DIR__);####Overriding built-in repositories
In the same way as above, you can override the built-in repository classes. Simply register your own class with the DI container using the built-in repo's identifier. This is not recommended however, as you may end up conflicting with other plugins.
3ev - Wordpress Core | http://www.3ev.com