Hi,
I'm writing to see if it's possible to add additional columns to the list display of a custom post created with WP-Types. I've attached a screenshot of the page that I'm talking about.
For example, all of these posts have a field for the Product ID. Is it possible to display the product ID on this page?
Thanks!
Dear Adams,
You have to add this code to your_theme_path/functions.php with some modifications:
//Add custom column
add_filter('manage_edit- YOURPOSTTYPENAME _columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['YOURCOLUMNNAME'] = 'YOURCOLUMNTITLE';
return $defaults;
}
//Add rows data
add_action( 'manage_ YOURPOSTTYPENAME _posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case ' YOURCOLUMNNAME ':
echo get_post_meta( $post_id , 'YOURCUSTOMFIELDNAME' , true );
break;
}
}
Where:
YOURPOSTTYPENAME - name of your post type slug
YOURCOLUMNNAME - name of your column, must be unique and without spaces. e.g. prodict_id
YOURCOLUMNTITLE - column title string.
YOURCUSTOMFIELDNAME - custom field name. e.g. wpcf-product_id
You can learn more at:
http://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Gen.
Hi Gen,
Thanks for the reply.
I'm able to get the column to display, but it's not populating with data.
Is YOURCOLUMNNAME arbitrary?
Here is the code that I'm using:
//Add custom column
add_filter('manage_edit-clearance-sale-item_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['clearance_product_id'] = 'Product ID';
return $defaults;
}
//Add rows data
add_action( 'manage_clearance-sale-item_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case 'clearance_product_id':
echo get_post_meta( $post_id , 'clearance-item-id-1' , true );
break;
}
}
I've double checked that I'm using the current slugs. Any idea why the data wouldn't be getting to the column?
Dear Adams,
in line: echo get_post_meta( $post_id , 'clearance-item-id-1' , true );
try to use wpcf-clearance-item-id-1
Also, you can check field names at:
hidden link
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Gen.