Admin Posts List (edit.php) by post IDs

I’m writing a plugin that creates posts in bulk. I provide a way for the user to set certain parameters of the created posts beforehand. But it would be useful to use the WordPress default “All Posts” (edit.php) interface to fine-tune the details after the posts are created. I had a look at the class-wp-list-table.php and duplicating its functionality is going to be a lot of work.

The best solution would be to just send the user to edit.php – limited only to the posts created by my plugin. I have the IDs of the posts, so I’m looking for a solution like edit.php?id=1&id=2, revealing a nice list table with only the posts with those IDs.

Related posts

Leave a Reply

1 comment

  1. It can be achieved using pre_get_posts.

    It is important to prefix all your variable names, id seems not be in the reserved terms list, but anyway this practice avoids any unforeseen bug.

    /**
     * Usage:
     * http://example.com/wp-admin/edit.php?my_pids=4088,4090,4092,4094
     */
    add_filter( 'pre_get_posts', 'limit_post_list_wpse_96418' );
    
    function limit_post_list_wpse_96418( $query ) 
    {
        // Don't run on frontend
        if( !is_admin() )
            return $query;
    
        global $pagenow;
    
        // Restrict to Edit page
        if( 'edit.php' !== $pagenow )
            return $query;
    
        // Check for our filter
        if( !isset( $_GET['my_pids'] ) )
            return $query;
    
        // Finally, filter
        $limit_posts = explode( ',', $_GET['my_pids'] ); // Convert comma delimited to array    
        $query->set( 'post__in', $limit_posts );      
    
        return $query;
    }