Library provides a functionality for associating your classes with WordPress class WP_Post. Once you create the instance, wpPostAble creates the WP_Post object and stores this in your instance.
You can manage your instance with such methods as
- $instance->getPost();
- $instance->getPostType();
- $instance->savePost();
- $instance->loadPost();
- $instance->getStatus();
- $instance->setStatus();
- $instance->getTitle();
- $instance->setTitle();
and others.
-
Create your own class based on wpPostAble interface
use iTRON\wpPostAble; class Item implements wpPostAble{ use wpPostAbleTrait; }
-
Call
wpPostAble()
method in the beginning__construct()
of your class.Pass to it two parameters
$post_type
string WP post type, associated with your class$post_id
int Post ID for existing post, or nothing for creating new post/** * @throws Exception\wppaLoadPostException * @throws Exception\wppaCreatePostException */ public function __construct() { $this->wpPostAble( POST_TYPE, $post_id ); // Do anything you need }
$item = new Item();
Once you create an instance, wpPostAble creates new post in WordPress as a draft.
Let's try change the title
$item->setTitle('The best item');
Now you have set title, and let's try to save it in database
$item->savePost();
Maybe it's time to publish?
$item->publish();
You can do it by single line
$item->setTitle('The best item')->publish();