How to filter / exclude posts by id in list of posts in WordPress Dashboard / Admin

So, what I am trying to do is filter the list of posts (mine is actually a custom post type) in the WordPress Dashboard by ID.

I am checking another area (custom widget) to see if the user can edit a given post (not, I am intentionally dodging the WordPress roles, etc), if they cannot I want to filter/exclude that post from the list.

Read More

I want to take this list:

See Image: https://lh6.googleusercontent.com/-nQLDUpoHUig/T84sUXwqNDI/AAAAAAAAB1o/fzZvCkSjawI/w678-h533-k/list_of_posts.PNG

…and filter out post ID’s that another function returns

Related posts

Leave a Reply

1 comment

  1. Okay, so I’ve answered my own question. Here is some code on how I did it.

    function exclude_list_per_function( $query ) {
    
        global $wpdb;
    
        //gets all the post ID's, I know this is a bit of a hack
        $querystr = "
            SELECT $wpdb->posts.ID
            FROM $wpdb->posts
        "; $post_ids = $wpdb->get_results($querystr, OBJECT);
    
            //Go through each post and pass it to a function that returns true if the user_can, and false if the user_can't
            foreach($post_ids as $post_obj){
                if(!can_user_other_function_view_this_post(get_post($post_obj->ID))){
                    //if they_can't, add them to the array to be excluded
                    $posts_not_in[]=$post_obj->ID;
                }
            }
    
            //Set those posts to be excluded from the list.
            $query->set( 'post__not_in', $posts_not_in );
    }
    
    add_action( 'pre_get_posts', 'exclude_list_per_function');