Advertisement
designbymerovingi

Custom Page Column: Page Template (Sortable)

Apr 21st, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. add_filter( 'manage_edit-page_columns',          'page_column_views' );
  2. add_filter( 'manage_edit-page_sortable_columns', 'page_column_views' );
  3. function page_column_views( $columns ) {
  4.  
  5.     $columns['page-layout'] = 'Template';
  6.     return $columns;
  7.  
  8. }
  9.  
  10. add_action( 'manage_pages_custom_column', 'page_custom_column_views', 5, 2 );
  11. function page_custom_column_views( $column_name, $page_id ) {
  12.  
  13.     if ( $column_name === 'page-layout' ) {
  14.  
  15.         $set_template = get_post_meta( $page_id, '_wp_page_template', true );
  16.  
  17.         if ( $set_template == 'default' ) {
  18.  
  19.             echo 'Default';
  20.  
  21.         }
  22.         else {
  23.  
  24.             $templates = get_page_templates();
  25.             ksort( $templates );
  26.             foreach ( array_keys( $templates ) as $template ) :
  27.                 if ( $set_template == $templates[$template] ) echo $template;
  28.             endforeach;
  29.  
  30.             echo '<br /><small>' . $set_template . '</small>';
  31.  
  32.         }
  33.     }
  34.  
  35. }
  36.  
  37. add_action( 'pre_get_posts', 'sort_pages_by_page_template' );
  38. function sort_pages_by_page_template( $query ) {
  39.  
  40.     if ( ! is_admin() )
  41.         return;
  42.  
  43.     $orderby = $query->get( 'orderby');
  44.  
  45.     if( 'page-layout' == $orderby ) {
  46.         $query->set( 'meta_key', '_wp_page_template' );
  47.         $query->set( 'orderby', 'meta_value' );
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement