Search code examples
wordpresstemplatescustom-post-type

How to change Admin Wordpress post list template for CPT?


I'm trying to change the back end post listing template for a custom post I added to wordpress. For clarity, the image below shows what I mean by "Post listing template":

image]

The regular post listing template shows "Title", "Author", "Categories", "Tags", "Languages", and "Date" fields. However, my custom post has extra functionality and other custom fields I'd like to add to this template, but I can't find the method.


Solution

  • Found the answer!
    The "manage__posts_columns" filter can be used for modifying the displayed columns.
    The "manage_posts_custom_column" action can be used for modifying the content of these columns.
    Code Example for adding custom columns:

    function change_columns( $cols ) {
      $cols = array(
        'cb'       => '<input type="checkbox" />',
        'title'      => __( 'Title',      'trans' ),
        'vin'      => __( 'Vin #',      'trans' ),
        'make' => __( 'Make', 'trans' ),
        'model'     => __( 'Model', 'trans' ),
        'year'     => __( 'Year', 'trans' ),
        'color'     => __( 'Color', 'trans' ),
        'thumbnail'     => __( 'Image', 'trans' ),
      );
      return $cols;
    }
    add_filter( "manage_tek_car_post_posts_columns", "change_columns" );
    

    Code Example for adding content to these columns:

    function custom_columns( $column, $post_id ) {
      switch ( $column ) {
        case "vin":
          $colvin = get_post_meta( $post_id, 'vin', true);
          echo $colvin;
          break;
        case "make":
          $colmake = get_post_meta( $post_id, 'carmake', true);
          echo $colmake;
          break;
        case "model":
          $colmodel = get_post_meta( $post_id, 'carmodel', true);
          echo $colmodel;
          break;
        case "year":
          $colyear = get_post_meta( $post_id, 'caryear', true);
          echo $colyear;
          break;
        case "color":
          $colcolor = get_post_meta( $post_id, 'excolor', true);
          echo $colcolor;
          break;
      }
    }
    
    add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
    

    Source: http://yoast.com/custom-post-type-snippets/