Replacing the List table of a Post Type

Is there a way to replace the WP_List_Table object of a post type to display said post type differently on the Admin edit.php page?

Related posts

2 comments

  1. No, you cannot replace the list table. There is no filter, everything is hard-coded.

    But you can change the post type registration, set show_ui to FALSE to prevent the built-in page, and add a custom page for the post type listing to show the editable items.

    add_action( 'wp_loaded', function(){
        register_post_type(
            'test',
            array(
                'labels' => array(
                    'name' => 'TEST'
                ),
                'public' => TRUE,
                'show_ui' => FALSE
            )
        );
    });
    
    add_action( 'admin_menu', function(){
        add_object_page(
            'TEST',
            'TEST',
            'edit_test',
            'test',
            function(){
                echo 'test'; // list post type items here
            }
        );
    });
    

    Result

    screen shot

  2. This example is applied to post post type. This is leveraging the WP_Posts_List_Table class and views-edit-{$post_type} filter. This seem isn’t the best way, but it works:

    Make sure the class is loaded on your page:

    if(!class_exists('WP_List_Table')){
        require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    }
    if(!class_exists('WP_Posts_List_Table')){
        require_once( ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php' );
    }
    

    Extend the WP_Posts_List_Table class to remove list table and define the custom content:

    class wpse_CustomTable extends WP_Posts_List_Table
    {       
        // remove search box
        public function search_box( $text, $input_id ){ }
    
        // Your custom list table is here
        public function display() {
            echo "Test";
        }
    }
    

    Use it inside a filter hook:

    // hook into `views-edit`
    add_filter( 'views_edit-post',  "sstssfb_custom_list_table");    
    // Override the post table object
    function sstssfb_custom_list_table() {
        global $wp_list_table;
        $mylisttable = new wpse_CustomTable();
        $wp_list_table = $mylisttable ;    
    }
    

    Result:

    enter image description here

Comments are closed.