hokoo / wppostable
Library provides a functionality for associating your classes with WordPress class WP_Post.
Installs: 254
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 3
pkg:composer/hokoo/wppostable
Requires
- ext-json: *
This package is auto-updated.
Last update: 2025-10-12 16:47:11 UTC
README
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.
How to use
-
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_typestring WP post type, associated with your class$post_idint 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 }
Now you are able to use your class
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.