I am currently working with WooCommerce, a eCommerce plugin for WordPress. WooCommerce products get setup in a new post-type called product. I have the following code which adds a custom column to the edit screen for this post type:
add_filter( 'manage_edit-product_columns', 'my_edit_product_columns' ) ;
function my_edit_product_columns( $columns ) {
$columns = array(
'myfield' => __( 'My field' )
);
return $columns;
}
This works fine, but unfortunatley it add's it as the last column. Is there a way to order the columns? I would like this column to be directly after the "Price Column"
$columns that is being passed to your function my_edit_product_columns is an array of all of the existing columns. You can replace the entire thing or use any standard array manipulation to change the columns & column order.
For example, if you wanted to specify the columns you would do something like (taken from an events custom post type I use):
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Event Name",
"event_date" => "Date",
"start_time"=>"Time",
);
So if you simply print_r($columns) to see what it currently has, you could manually reorder it.
To insert your column at a specific position in the existing $columns array use:
# Insert at offset 2
$offset = 2;
$newArray = array_slice($columns, 0, $offset, true) +
array('new_column_id' => 'New Column Name') +
array_slice($columns, $offset, NULL, true);
See this thread for more on that: array_splice() for associative arrays
//ADDED
I just tested this out locally on a custom post type I have in use called products. This code works fine for me. The offset starts with column 1, so to make my new column be the second one, I set offset at 2.
public function productsListColumns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Product",
"price" => "Price"
);
$offset = 2;
$newArray = array_slice($columns, 0, $offset, true) +
array('new_column_id' => 'New Column Name') +
array_slice($columns, $offset, NULL, true);
return $newArray;
}
Please check the following code. You just need to replace your post type with TribeEvents::POSTTYPE
add_filter('manage_' . TribeEvents::POSTTYPE . '_posts_columns', 'column_headers');
add_action( 'manage_posts_custom_column', 'custom_columns' , 10, 2 );
function column_headers($columns)
{
$columns['tickets-attendees'] = __( 'Attendees', 'tribe-events-calendar');
return $columns;
}
function custom_columns( $column_id, $post_id ) {
if ( $column_id == 'tickets-attendees' ) {
//echo $post_id;
$items = TribeEventsTickets::get_event_attendees($post_id );
$attendeeCount = count($items);
?>
<a href = "edit.php?post_type=tribe_events&page=tickets-attendees&event_id=<?php echo $post_id; ?>"><?php echo $attendeeCount; ?></a>
<?php
}
}