PostType class helps you to register and manipulate WordPress custom post types.
In order to register a custom post type, you will call the
set methods of the PostType class.
For example, let's register a custom post type in order to handle a list of books. The custom post type will have a slug of
// File stored in resources/admin/books.php PostType::make('books', 'Books', 'Book')->set([ 'public' => true ]);
This will build a basic custom post type accessible in the WordPress admin. You can customize the custom post type by passing arguments to the
set() method. In the above code we specified that the custom post type should be public. You can pass all the arguments defined in the WordPress core function register_post_type like so:
PostType::make('slug-books', 'Books', 'Book')->set([ 'public' => true, 'menu_position' => 20, 'supports' => ['title'], 'rewrite' => false, 'query_var' => false ]);
Note: the custom post type will only be registered if you call the
make() method needs a plural and singular display names as second and third parameter. Those parameters will pre-fill the labels property of your custom post type.
PostType::make('books', 'Books', 'book')->set();
In the above code sample, the custom post type will have a plural display name of
Books and a singular display name of
Note: you can override the custom post type
labelsproperty by adding it to the set method array.
In order to define the behavior of your custom post type, use the
set() method and pass it an array of parameters like so:
PostType::make('books', 'Books', 'Book')->set([ 'public' => false, 'supports' => ['title', 'editor'], 'labels' => [ 'add_item' => __('Add', 'THEME_TEXTDOMAIN') ] ]);
THEME_TEXTDOMAINconstant value is defined inside the
get() method allows you to retrieve parameters value from your custom post type that you defined in the
set() method. Here is a list of what you can grab:
Note: this methods replace the deprecated method $postType->getSlug(). If you used this method, make sure to update your code if you use a version of the framework higher or equal to 1.2.0.
Those parameters are default to all custom post type. But if you have defined other parameters and passed them through the set() method, you can grab them too.
Example with a default parameter:
$postType = PostType::make('books', 'Books', 'Book')->set(); // Return "books". $name = $postType->get('name');
If you try to access a parameter other than the list above and that it's not defined in the set method, you'll get an error.
Now let's say we define a custom URI slug for our
books custom post type like so:
$postType = PostType::make('books', 'Books', 'Book')->set([ 'rewrite' => [ 'slug' => 'library' ] ]); // We can retrieve the rewrite parameter as it is defined in the set method. $rewriteParameters = $postType->get('rewrite'); $slug = $rewriteParameters['slug']; // return the 'library' value.
WordPress now returns an object when registering a custom post type. In order to get access to this instance, use the
instance() method like so:
$books = PostType::make('books', 'Books', 'Book')->set(['public' => true]); $type = $books->instance(); // return WP_Post_Type instance
The PostType API provides a method to register one or multiple custom statuses for your custom post type.
Note: This do not work for core post types (post, page, attachment,...).
When you add custom statuses, the default statuses are removed except the
When you add custom statuses, they appear in the order you defined them. And the first defined status is the one used in order to "publish/register/post" your custom post type. So basically, the first defined status is acting like the core "Publish" button or simply is the first status to be saved for your custom post type except if you choose to draft it.
In this example, keeping the books custom post type, we want to build a system to rent and sell books. So we need to register a status to say that a book is available for rent, is currently rented or for sell. Plus by getting advantage of the default draft status, you can save your work for later.
status() method uses the same arguments than the WordPress register_post_status() function. But you can also pass an array of statuses.
First, let's add one custom status
// Register the custom post type first. $books = PostType::make('books', 'Books', 'Book')->set(); // Register the "rent" custom status. $books->status('rent');
In the example above, one status
rent is registered. The method assign default properties to the status, the same properties used in the register_post_status() function:
namewith first character set to uppercase
_n_noop()function with status label
__('Apply Changes'). This property allows you to define a custom text for the publish button and per status.
In the following example, we add a custom publish button text to our
$books->status('rent', [ 'publish_text' => __('Rent the book', THEME_TEXTDOMAIN) ]);
The code above will change the default button text of Publish to Rent the book when a user is registering its book from the edit screen.
Using the same
status() method on your custom post type, you can add more than one status. Just pass an array of custom statuses like so:
// Register the custom post type first. $books = PostType::make('books', 'Books', 'Book')->set(); // Add our custom statuses: rent, rented, sell, sold $books->status([ 'rent', 'rented', 'sell', 'sold' ]);
These statuses are registered with default properties. If you create a new book in the WordPress administration, you should see this list of statuses inside the publish metabox: Draft, Rent, Rented, Sell and Sold.
You can also define their properties like so:
$books->status([ 'rent' => [ 'publish_text' => 'Save and rent the book' ], 'rented' => [ 'publish_text' => 'Set the book as rented' ], 'sell' => [ 'publish_text' => 'Sell the book' ], 'sold' => [ 'publish_text' => 'Set the book as sold' ] ]);
The array key is the custom status name and each value is an array of status properties.
Note: Currently the UI for custom statuses is a work-in-progress. When viewing the list of your custom post type, if you click on
Quick Edit, it still displays core statuses.
You can read the following documentation:
Made in Belgium