How do I format custom columns in the list view for custom post types? (3 posts)

  1. cynicaloptimist
    Posted 5 years ago #

    I have the following code, which works approximately as expected:

    add_filter('manage_knowledgebase_posts_columns', 'wpkb_columns' );
        function wpkb_columns( $defaults ) {
        	$defaults['upvotes'] = __('<strong>U</strong>');
        	$defaults['downvotes'] = __('<strong>D</strong>');
        	return $defaults;
        add_action( 'manage_knowledgebase_posts_custom_column', 'wpkb_custom_columns' );
        function wpkb_custom_columns( $column ){
        	global $post;
        	switch ( $column )
        		case 'upvotes':
        			$upvotes = count( get_post_meta( $post->ID, 'upvote' ) );
        			echo $upvotes;
        		case 'downvotes':
        			$downvotes = count( get_post_meta( $post->ID, 'downvote' ) );
        			echo $downvotes;

    The edit.php page shows the upvotes/downvotes columns as fat, unsortable columns on the right hand side. I'd like to do the following:

    • Specify column width
    • Specify column order
    • Make columns sortable

    What's the best way to do this?

    [Cross posted from stack overflow]

  2. cynicaloptimist
    Posted 5 years ago #

    I managed to find a solution to the sorting problem at http://scribu.net/wordpress/custom-sortable-columns.html. An important caveat is that the 'meta_key' member of the $vars[] array is referring to a specific post_meta value in the database, which is only obvious in retrospect. It didn't work at first with my code since I wasn't storing the total number of votes in post_meta, just each individual vota (and its associated IP).

    I'm still trying to figure out how to make the columns appear more nicely. Might be a stylesheet problem.

  3. cynicaloptimist
    Posted 5 years ago #

    Update 2: The columns were easily style with the stylesheet, as expected. I never found a way to make my own columns appear to the left of the WordPress core columns, and I believe this is intended behavior.

Topic Closed

This topic has been closed to new replies.

About this Topic