-
Notifications
You must be signed in to change notification settings - Fork 2
Custom Table Columns
H::add_column( $post_type, $column_slug, $column_args );
Append one column to the current table.
PARAMETERS
$post_type
(string) - In which post table to append this column.
$column_slug
(string) - The column's ID.
$column_args
(array / optional) - The column's configuration. You can ignore this param if your slug
is one of the Reserved slugs. Everything will be automatically populated.
-
name
(string) - The shown label.Default: capitalized version of the slug.
-
content
(function) - Return the column's content. Parameters:($post, $custom_fields)
.$custom_fields
is gained fromget_post_custom()
. -
sortable
(boolean) - Enable sorting, can only be alphabetically or numerically.Default: false
-
icon
(string) - Replace the label with icon. Use the icon name from dashicons.Default: empty
-
position_before
(string) - Where to append the column.Default: 'date' (before Published Date)
-
position_after
(string) - Where to prepend the column.Default: empty
You have 2 ACF Fields called start_date
and end_date
. You want to print with the format: "10 Apr - 15 Apr 2020".
H::add_column( 'post', 'start-end-date', [
'name' => 'Start / End Date',
'position_after' => 'categories',
'content' => 'column_start_end_date'
] );
function column_start_end_date( $post, $fields ) {
if( !isset( $fields['start_date'] ) ) {
return '-';
}
$start_date = DateTime::createFromFormat( 'Ymd', $fields['start_date'][0] );
$end_date = DateTime::createFromFormat( 'Ymd', $fields['end_date'][0] );
return $start_date->format( 'd M' ) . ' - ' . $end_date->format( 'd M Y' );
}
Using one of the reserved slug means the content is automatically populated. They are:
-
Built-in Columns
-
title
- Post's title with edit, quick edit, etc links author
-
thumbnail
- Featured image 75x75 size content
comments
-
date
- Published date
-
-
Taxonomy Slug like
category
.- List all terms in comma-separated string.
-
ACF Custom Field
H::override_columns( $post_type, $columns );
Override all the table columns with your list.
PARAMETER
$post_type
(string)
$columns
(array) - Collection of column's arguments. Same list as above, but don't use position_before
and position_after
.
In our Product post type, we have price
and discount_percent
custom field.
We want to have a table with these columns:
- Product Thumbnail
- Title
- Price
- Discount % and how much you save
- Published Date
H::override_columns('product', [
'thumbnail' => [], // 'thumbnail' is a reserved slug, so no args needed.
'title' => [],
'price' => [], // 'price' is also reserved since it's the exact name of our ACF Field.
'discount' => [
'name' => 'Discount',
'content' => 'show_discounted_price'
]
'date' => [],
]);
function show_discounted_price( $post, $fields ) {
$discount = isset( $fields['discount'] ) ? $fields['discount'][0] : null;
$price = isset( $fields['price'] ) ? $fields['price'][0] : null;
$total = $price - ($price * $discount / 100);
$saving = $price - $total;
return $discount . '% Discount - You save ' . $saving;
}