Support » Plugin: Custom Post Type UI » Adding custom fields columns to CPT list

  • Resolved Chris

    (@osiak)


    Hi,
    I wanted to have extra columns on my custom posts list after creating a new post type (“Trips” in my case) in CPTUI and have these columns pull data from custom fields created by ACF. I have used this code:

    add_filter( 'manage_edit-trips_columns', 'my_edit_trips_columns' ) ;
    
    function my_edit_trips_columns( $columns ) {
    
    	$columns = array(
    		'cb' => '<input type="checkbox" />',
    		'title' => __( 'Trip name' ),
    		'region' => __( 'Region' ),
    		'start_date' => __( 'Start date' ),
    		'date' => __( 'Date' )
    	);
    
    	return $columns;
    }
    
    add_action( 'manage_trips_posts_custom_column', 'my_manage_trips_columns', 10, 2 );
    
    function my_manage_trips_columns( $column, $post_id ) {
    	global $post;
    
    	switch( $column ) {
    
    		case 'region' :
                echo get_post_meta( $post_id , 'region' , true ); 
                break;
    
            case 'start_date' :
                echo get_post_meta( $post_id , 'start_date' , true ); 
                break;
    
        }
    }

    This showed my columns and respective values beautifly. I also wanted also a column with the custom taxonomies to show. I created my custom taxonomies and selected the “Show Admin Column” setting to True. However the taxonomy column didn’t show up, alhtough it did when I disabled the above code. Is there a way to have the custom columns display AND the custom taxonomy column as well?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m putting my bets on the my_edit_trips_columns() function is receiving a $columns array that has that taxonomy column in it, but since you’re completely redefining the array, you’re returning one that doesn’t have it present.

    Perhaps try something like this instead, to overwrite and append indexes, without redefining the whole array.

    $columns['title'] = __( 'Trip name' );
    $columns['region'] = __( 'Region' );
    $columns['start_date'] = __( 'Start date' );
    

    I’m not a great JS guy – can you help me redefine the whole code and how/where to cut/paste your insert?

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    add_filter( 'manage_edit-trips_columns', 'my_edit_trips_columns' ) ;
    function my_edit_trips_columns( $columns ) {
    
    	$columns['title'] = __( 'Trip name' );
    	$columns['region'] = __( 'Region' );
    	$columns['start_date'] = __( 'Start date' );
    
    	return $columns;
    }
    
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Had to edit cause i realized I copied the wrong add_filter line.

    Works great! However my publish date column is now in the middle, right after the name. Any chance to make it go back as the last column?

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    You’ll need to do some juggling for that. This isn’t fully tested, but a different mimicked type up did for me.

    add_filter( 'manage_edit-trips_columns', 'my_edit_trips_columns' ) ;
    function my_edit_trips_columns( $columns ) {
    
    	// store value temporarily.
    	$date_val = $columns['date'];
    
    	// Unset original index.
    	unset( $columns['date'] );
    
    	$columns['title'] = __( 'Trip name' );
    	$columns['region'] = __( 'Region' );
    	$columns['start_date'] = __( 'Start date' );
    	$columns['date'] = $date_val;
    
    	return $columns;
    }
    

    It assumes that “date” will always be one of the indexes.

    The date column does not show now…

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not sure what to tell you for that detail. Here’s a screenshot of a “portfolio” post type that I had readily available, which is showing all the intended columns, using my code above. No values for anything, since there are no posts at the moment 😀

    Screen Shot

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.