Return to Snippet

Revision: 59998
at October 14, 2012 19:36 by leoken


Initial Code
// Change the columns for the edit CPT screen
function change_columns( $cols ) {
  $cols = array(
    'cb'       => '<input type="checkbox" />',
    'url'      => __( 'URL',      'trans' ),
    'referrer' => __( 'Referrer', 'trans' ),
    'host'     => __( 'Host', 'trans' ),
  );
  return $cols;
}
add_filter( "manage_<CPT>_posts_columns", "change_columns" );


function custom_columns( $column, $post_id ) {
  switch ( $column ) {
    case "url":
      $url = get_post_meta( $post_id, 'url', true);
      echo '<a href="' . $url . '">' . $url. '</a>';
      break;
    case "referrer":
      $refer = get_post_meta( $post_id, 'referrer', true);
      echo '<a href="' . $refer . '">' . $refer. '</a>';
      break;
    case "host":
      echo get_post_meta( $post_id, 'host', true);
      break;
  }
}

add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );

// Make these columns sortable
function sortable_columns() {
  return array(
    'url'      => 'url',
    'referrer' => 'referrer',
    'host'     => 'host'
  );
}

add_filter( "manage_edit-<CPT>_sortable_columns", "sortable_columns" );

// Filter the request to just give posts for the given taxonomy, if applicable.
function taxonomy_filter_restrict_manage_posts() {
    global $typenow;

    // If you only want this to work for your specific post type,
    // check for that $type here and then return.
    // This function, if unmodified, will add the dropdown for each
    // post type / taxonomy combination.

    $post_types = get_post_types( array( '_builtin' => false ) );

    if ( in_array( $typenow, $post_types ) ) {
    	$filters = get_object_taxonomies( $typenow );

        foreach ( $filters as $tax_slug ) {
            $tax_obj = get_taxonomy( $tax_slug );
            wp_dropdown_categories( array(
                'show_option_all' => __('Show All '.$tax_obj->label ),
                'taxonomy' 	  => $tax_slug,
                'name' 		  => $tax_obj->name,
                'orderby' 	  => 'name',
                'selected' 	  => $_GET[$tax_slug],
                'hierarchical' 	  => $tax_obj->hierarchical,
                'show_count' 	  => false,
                'hide_empty' 	  => true
            ) );
        }
    }
}

add_action( 'restrict_manage_posts', 'taxonomy_filter_restrict_manage_posts' );

Initial URL

                                

Initial Description

                                

Initial Title
Add columns to admin page for a Custom Post Type 

Initial Tags
wordpress

Initial Language
PHP