Add columns to admin page for a Custom Post Type


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // Change the columns for the edit CPT screen
  2. function change_columns( $cols ) {
  3. $cols = array(
  4. 'cb' => '<input type="checkbox" />',
  5. 'url' => __( 'URL', 'trans' ),
  6. 'referrer' => __( 'Referrer', 'trans' ),
  7. 'host' => __( 'Host', 'trans' ),
  8. );
  9. return $cols;
  10. }
  11. add_filter( "manage_<CPT>_posts_columns", "change_columns" );
  12.  
  13.  
  14. function custom_columns( $column, $post_id ) {
  15. switch ( $column ) {
  16. case "url":
  17. $url = get_post_meta( $post_id, 'url', true);
  18. echo '<a href="' . $url . '">' . $url. '</a>';
  19. break;
  20. case "referrer":
  21. $refer = get_post_meta( $post_id, 'referrer', true);
  22. echo '<a href="' . $refer . '">' . $refer. '</a>';
  23. break;
  24. case "host":
  25. echo get_post_meta( $post_id, 'host', true);
  26. break;
  27. }
  28. }
  29.  
  30. add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
  31.  
  32. // Make these columns sortable
  33. function sortable_columns() {
  34. return array(
  35. 'url' => 'url',
  36. 'referrer' => 'referrer',
  37. 'host' => 'host'
  38. );
  39. }
  40.  
  41. add_filter( "manage_edit-<CPT>_sortable_columns", "sortable_columns" );
  42.  
  43. // Filter the request to just give posts for the given taxonomy, if applicable.
  44. function taxonomy_filter_restrict_manage_posts() {
  45. global $typenow;
  46.  
  47. // If you only want this to work for your specific post type,
  48. // check for that $type here and then return.
  49. // This function, if unmodified, will add the dropdown for each
  50. // post type / taxonomy combination.
  51.  
  52. $post_types = get_post_types( array( '_builtin' => false ) );
  53.  
  54. if ( in_array( $typenow, $post_types ) ) {
  55. $filters = get_object_taxonomies( $typenow );
  56.  
  57. foreach ( $filters as $tax_slug ) {
  58. $tax_obj = get_taxonomy( $tax_slug );
  59. wp_dropdown_categories( array(
  60. 'show_option_all' => __('Show All '.$tax_obj->label ),
  61. 'taxonomy' => $tax_slug,
  62. 'name' => $tax_obj->name,
  63. 'orderby' => 'name',
  64. 'selected' => $_GET[$tax_slug],
  65. 'hierarchical' => $tax_obj->hierarchical,
  66. 'show_count' => false,
  67. 'hide_empty' => true
  68. ) );
  69. }
  70. }
  71. }
  72.  
  73. add_action( 'restrict_manage_posts', 'taxonomy_filter_restrict_manage_posts' );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.