Using hooks how can add new columns on list page for custom post type in Admin


Hello Friends!

If you want to add a new columns with custom meta fields on list page for any post type then you can do it using the wordpress hooks.

Using hooks you have no need to edit in wordpress core file, you can do all by edit in theme function.php file.

add_filter( 'manage_edit-news_columns', 'news_edit_columns_title' ); 
//Here news is a custom post type , you can update its value according to post type.
/* We are taking zipcode field as custom filed */
function news_edit_columns_title( $columns ) {
 $columns = array(
 'cb' => '<input type="checkbox" />',
 'title' => __( 'Title', 'Theme_Name' ),
 'zipcode' => __( 'Zipcodes', 'Theme_Name' ),
 'date' => __( 'Date', 'Theme_Name' ),
 );
 return $columns;
}

/* Set the zipcode field value to that columns */

add_action( 'manage_posts_custom_column', 'add_news_columns_value' );
function add_news_columns_value( $column ) {
$custom_fields = get_post_custom();

 switch ( $column ) {
 case 'zipcode' :
 echo get_post_meta( get_the_ID(), 'custom_zipcode_value_key',true );
//here custom_zipcode_value_key is the meta key for zipcode
break;
 }
}

Screenshot-1
You can visit on wordpress official site for know more about manage_posts_custom_column & manage_edit-post_type_columns functions.

http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_edit-post_type_columns
http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

Hope you all will be enjoy my code!

Advertisements

Sr. Software Engineer

Tagged with: , , ,
Posted in Wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: