Post Types Personalizados
Onyx provides the possibility of creating custom Post Types that can be done in two ways. The first through this file ./core/config/cpts.php
and the other is by instantiating an object of the class \Onyx\Cpt()
.
Parameters
The only Required parameter is the name(s)
of the post type and can be a string as we can see in Example 1 or an array as in Example 2. Onyx will automatically extract slug
, labels
, options
if they are not provided as well as try to apply plural to the necessary labels.
caution
If not provided, the parameter slug
will be used to create the post type and it is the one that should be used for queries and relationships. By default, slug
is extracted from the value name
transformed to the plural.
Ex: 'Custom Product' plural will be 'Custom Products' and slug 'custom-products'.
Param | Type | Description | Required |
---|---|---|---|
name(s) | string/array | Post Type name | ✔ |
icon | string | Post Type icon | ✘ |
labels | array | Custom labels | ✘ |
options | array | Operating arguments | ✘ |
filters | array | Admin search filters (selects for taxonomies) | ✘ |
columns | array | Register custom columns in the admin | ✘ |
info
To learn about options for labels
and options
, visit the WordPress instructions register_post_type() and get_post_type_labels()
info
When adding parameters like labels
or options
, you do not need to include all options. Only the parameters you determine will be replaced in the model.
Creating Post Types
Using this method, you don't need to instantiate the class \Onyx\Cpt()
.
Search Filters
When registering a custom Taxonomy, you can also add search filters to the WordPress admin listing table
Custom Admin Columns
You can include, remove or reorder custom columns in the WordPress admin listing table when registering a Post Type. This is very useful when you have custom fields or custom taxonomies
info
To add columns for taxonomies, see entry in Taxonomy.
Param | Type | Description | Required |
---|---|---|---|
add | array | Columns to add | ✘ |
order | array | Columns order | ✘ |
hide | array | Remove Columns | ✘ |
add
Param | Type | Description | Required |
---|---|---|---|
label | string | Column label | ✔ |
populate | callable | Function that receives 2 values fn($column, $post_id) | ✔ |
sort | string | Meta Key for sorting | ✘ |
numeric | bool | Sort via numeric or string | ✘ |
order
An array with the key or column name
. The columns will appear in the order of inclusion within the array.
hide
An array with the key or column name
. Columns added here will be removed from the admin list table.
Example of use
Instantiating a Post Type
In addition to being able to create a Post Type using the record in the ./core/config/cpts.php
file, you can instantiate an object and manually create its CPT using the class \Onyx\Cpt
info
only the parameter name(s)
is required, it can either be a string or an array.