Library provides a functionality for associating your models with WordPress WP_Post model. Once you create the instance, wpPostAble creates the WP_Post object and stores it in your instance.
You can manage your instance with such methods as
$instance->getTitle();
$instance->setTitle();
$instance->getMetaField();
$instance->setMetaField();
$instance->getStatus();
$instance->setStatus();
$instance->getPost();
$instance->getPostType();
$instance->savePost();
$instance->loadPost();
$instance->publish();
$instance->draft();
and others.
Use
$instance->getParam();
$instance->setParam();
method to manage metafields, stored inside posts
table using post_content_filtered
field.
-
Create your own class based on wpPostAble interface
use iTRON\wpPostAble\wpPostAble; use iTRON\wpPostAble\wpPostAbleTrait; use iTRON\wpPostAble\Exceptions\wppaCreatePostException; use iTRON\wpPostAble\Exceptions\wppaLoadPostException; class Item implements wpPostAble { use wpPostAbleTrait; const POST_TYPE = 'item'; }
-
Call
wpPostAble()
method in the beginning of the__construct()
method 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( int|null $post_id = null ) { $this->wpPostAble( self::POST_TYPE, (int) $post_id ); // Do anything you need }
Create new post
$item = new Item();
or load from existing one
$item = new Item( $post_id );
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();
More options you can find in the description above and in the source code.