Managing Columns - allows you to remove, reorder and add columns
manage_[post_type]_posts_columns
//Customizing Admin Columns In WordPress
add_filter('manage_em_banner_posts_columns', 'filter_posts_columns');
function filter_posts_columns($columns)
{
$columns = array(
'cb' => $columns['cb'],
'title' => __('Title'),
'image' => __('Image'),
'taxonomy-banner_category' => __('Banner Categories'),
'date' => __('Date'),
'shortcode' => __('Shortcode'),
'link' => __('Has Link'),
);
// $columns['image'] = __('Image');
return $columns;
}
Setting up columns - populating columns - just check for conditional statement to match the defined column
manage_[post_type]_posts_custom_column
add_action('manage_em_banner_posts_custom_column', 'em_banner_column', 10, 2);
function em_banner_column($column, $post_id)
{
// Image column
if ('image' === $column) {
$acf_img = get_field('banner_image', $post_id);
// var_dump($acf_img['sizes']);
$img_large = $acf_img['sizes']['large'];
echo "<img src='" . $img_large . "' width='auto' height='80' style='max-width: 420px;height: auto;max-height: 80px;' />";
}
// Shortcode column
if ('shortcode' === $column) {
echo "<input type='text' value='[em_banner id=" . $post_id . "]' readonly='readonly' />";
}
// link column
if ('link' === $column) {
if (get_field('banner_link_url', $post_id)) {
echo "Yes";
} else {
echo "No Link Yet";
}
}
}
Making columns sortable is relatively easy in WordPress, yet it's rarely implemented for custom columns. Sorting helps you organize content and enables you to find content quickly. For example, we could sort our real-estate overview by price and then find our cheapest and most expensive pieces of property.
We'll start by adding our "price" column to the list of sortable columns:
add_filter( 'manage_edit-realestate_sortable_columns', 'smashing_realestate_sortable_columns');
function smashing_realestate_sortable_columns( $columns ) {
$columns['price'] = 'price_per_month';
return $columns;
}
This code hooks into the manage_edit-realestate_sortable_columns
filter and adds smashing_realestate_sortable_columns
as a callback function (line 1). The "price" column is made sortable by adding it to the list of sortable columns in line 3. Note that the array key, price
, is the name we've given our column before. The array value is used to tell WordPress what it should sort by. We could, for example, use WordPress' native sorting strategies to sort by title, date or comment count.
In our case, however, we want to sort by a custom field. This requires us to create another function, which needs to alter the posts query if we're trying to sort by price. We're going to use the pre_get_posts
action. It allows us to change the WP_Query
object (the object that WordPress uses to query posts) and fires before posts are queried. We check whether to sort by price and, if so, change the query accordingly:
add_action( 'pre_get_posts', 'smashing_posts_orderby' );
function smashing_posts_orderby( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'price_per_month' === $query->get( 'orderby') ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'price_per_month' );
$query->set( 'meta_type', 'numeric' );
}
}
Lines 1 and 2 add the callback function to the action and start the definition of the callback function, respectively. Line 3 then checks whether we're in the admin panel and whether the query is the main posts query (see the Codex for more information). If not, we don't change the query. Then, we check whether the current sorting strategy is by price. If it is, we adjust the query accordingly: We set the meta_key
value to price_per_month
, telling WordPress to retrieve this custom field for all real estate, and we set the orderby
key to meta_value
, which tells WordPress to sort by the value belonging to the specified meta key. Finally, we tell WordPress to sort numerically instead of alphabetically, because "price" is a numerical column.
Note: You might have noticed that the filter we hook into is named manage_edit-realestate_sortable_columns
, whereas you might have expected it to be manage_realestate_sortable_columns
(without the edit-
). This is simply an inconsistency in filter naming in WordPress.
The same procedure can be repeated for the "area" column. For textual columns, which should be sorted alphabetically, be sure to leave out the $query->set( 'meta_type', 'numeric' )
part.