Custom Post Type
Declare new custom post types in inc/fx-cpt.php
To make a new post type, create a new instance of the CPT Class.
$new_post_type = new CPT($slug: string, $options: array);
The Class is a wrapper around the Wordpress register_post_type method. So all options, even if unlisted, will be passed on.
Example
$events = new CPT('event', [
'singular' => 'Event',
'plural' => 'Events',
'taxonomies' => ['category'],
'icon' => 'f508',
]);
Options
The options array have the following shape:
$options = [
singular: string,
plural: string,
breadcrumb: string,
icon: string,
hierarchical: bool,
taxonomies: [],
exclude_from_search: bool,
];
Find a list of available icons here.
Admin Columns
Custom Admin Columns
You can add custom columns to the CPT admin page using the add_column method after. You can add as many columns as you want, just provide an id, title and callback to get it working.
// Create the class
$custom_post_type = new CPT();
// Add a new section for custom meta
$custom_post_type->add_column($id: string, $title: string, $callback: ($post) => string, $sort: boolean|function($query));
Here's a working example:
// Add the column to the edit events page
$events->add_column('event_venue', 'Gallery Venue', function($post) {
// This is the callback function which populates the column value
// You have the Wordpress $post object
$post_id = $post->ID;
// Get associated meta
$venue = fx_get_meta('venue');
// Then render the value
echo $venue;
}, true);
Note: You can use the callback function to show anything, the example just shows how you can show custom meta on the edit page.
You can make admin columns sortable by passing either true or a function($query). Use a function to handle more complex sorting options.
Here's a working example using a sorting function over two meta values:
// Add a column to an event booking and sort by date and time
$booking->add_column('booking', 'Booking', function($post) {
$date = fx_get_meta('date', $post->ID);
$time = fx_get_meta('time', $post->ID);
echo $date . ' - ' . $time;
}, function($query) {
// Check what the sort order is
$sort_order = $_GET['order'] === 'asc' ? 'ASC' : 'DESC';
// First get exisiting meta queries occurring (e.g. filters)
$current_meta_query = $query->get('meta_query') ?: array();
// Add your meta query to the exisiting
$query->set('meta_query', array_merge($current_meta_query, array(
'relation' => 'AND',
'booking_date' => array(
'key' => '_fx_date_value',
'type' => 'numeric',
),
'booking_time' => array(
'key' => '_fx_time_value',
'type' => 'time',
),
)));
// Now set the query to order by the two meta keys in this order
$query->set('orderby', array(
'booking_date' => $sort_order,
'booking_time' => $sort_order,
));
});
Note: The sorting function is given the current $query object which you will just manipulate.