1

enter image description here

So my question is pretty much what you see in the picture. In my custom post type of "Products" how can I add an extra column like SEO Yoast did to my main table which is called WP_List_Table. I have the code figured out as to how I will get an out of stock feature to show up depending on if the item has a custom_field of "Out of Stock" set to true or false. Thats not problem but how can I target that area of the table in my code. I would assume that it would be a filter, not to sure on how that would work but id really appreciate it if someone can help me.

1

I think what you need is the set of filters and actions to add custom columns to post edit screens:

  • manage_edit-post_type_columns: used to add columns
  • manage_posts_custom_column: used to print the content of each row of the column
  • manage_edit-post_type_sortable_columns: used to register sortable columns. Undocumented.
  • request: you may need to use this filter in combination with manage_edit-post_type_sortable_columns.

For example, I use this code to add a sortable column with post hits information from a custom meta field:

add_filter( 'manage_edit-post_columns',  'add_new_columns' );
add_filter( 'manage_edit-post_sortable_columns', 'register_sortable_columns' );
add_filter( 'request', 'hits_column_orderby' );
add_action( 'manage_posts_custom_column' , 'custom_columns' );
/**
* Add new columns to the post table
*
* @param Array $columns - Current columns on the list post
*/
function add_new_columns($columns){

    $column_meta = array( 'hits' => 'Hits' );
    $columns = array_slice( $columns, 0, 6, true ) + $column_meta + array_slice( $columns, 6, NULL, true );
    return $columns;

}

// Register the columns as sortable
function register_sortable_columns( $columns ) {
    $columns['hits'] = 'hits';
    return $columns;
}

//Add filter to the request to make the hits sorting process numeric, not string
function hits_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'hits' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'hits',
            'orderby' => 'meta_value_num'
        ) );
    }

    return $vars;
}

/**
* Display data in new columns
*
* @param  $column Current column
*
* @return Data for the column
*/
function custom_columns($column) {

    global $post;

    switch ( $column ) {
        case 'hits':
            $hits = get_post_meta( $post->ID, 'hits', true );
            echo (int)$hits;
        break;
    }
}
  • Looks great! How would you make the additional key you add to the array which is the extra column first to the left. Like when you see the wp_list_table the fist is title. How can I make my custom column first? – Lucas Santos Feb 11 '15 at 13:58
  • In function add_new_columns($columns), the var $columns is an array and you can insert the new column in the position you want using normal PHP code for that. I use array_slice to insert my custom column in the 6th position but you could use any other PHP array manipulation thecnique. For example array_unshift to prepend a element to the beginning of an array. But this is a pure PHP question that may be more suitable for StackOverflow than WPSE. – cybmeta Feb 11 '15 at 16:46
  • Ok got it, the part that was giving me trouble was what function I had to manipulate to array in. – Lucas Santos Feb 12 '15 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.