How to add columns in a Custom Post Type list in Admin.
-
Hi,
I have created a Custom Post Type ‘ads’, which is working fine on both admin and user end. Now I have to create a couple of columns in the CPT default listing page in WP admin.
I have created label for the fields like this:
function adm_ads_table_head( $defaults ) {
$defaults[‘ad_expire_date’] = ‘Expiry Date’;
$defaults[‘ad_clicks’] = ‘Clicks’;
return $defaults;
}and the function is hooked like this:
add_filter(‘manage_ads_posts_columns’, array( $this, ‘adm_ads_table_head’));
so far so good. Now I have to set content for the above columns. I have used this simple function to make it work.
function adm_ads_table_content( $column_name, $post_id ) {
if ($column_name == ‘ad_expire_date’) {
//echo ‘121212’;
//echo get_metadata(‘ads’, $post_id, ‘_ad_dtexp’, true);
echo get_post_meta( $post_id, ‘_ad_dtexp’, true );
}
}and this is the hook:
add_action( ‘manage_ads_posts_custom_column’, array( $this, ‘adm_ads_table_content’) );
The problem is when i echo any static value like ‘121212’ it successfully shows in the Expiry Date column. But when i try to use
get_metadata or get_post_metadata, nothing displays in that column. Please help!!!
baburman
- The topic ‘How to add columns in a Custom Post Type list in Admin.’ is closed to new replies.