How to add pagination after using hook_views_post_execute() to modify $view->result? I have successfully used hook_views_post_execute() to remove results I do not want from $views-&gt;result.

Now, however, I need to paginate the remaining results.

The code in my hook_views_post_execute() requires the full result set to work. If I edit the view in the admin interface and set it to 10 items per page with full pagination, the $views-&gt;result in hook_views_post_execute() will only include 10 items, so my code doesn't function properly.

It looks like I need to use hook_views_pre_render() to set up the pagination, but I'm not sure how.

Can someone point me in the right direction?
I eventually realized that hook_views_query_alter() was the appropriate hook, but it wasn't working because this view utilizes Search API. Turns out hook_search_api_views_query_alter() is a hook specifically for altering the query when using Search API.

I used this hook to add a condition to the view query before it is executed. This allowed me to turn pagination back on in the admin panel and have it work as anticipated.

function mymodule_search_api_views_query_alter(view &amp;$view, SearchApiViewsQuery &amp;$query) {
  if ($view-&gt;name === 'my_view_name') {
    // Do some stuff to get the nids I'd like to allow in my result set.
    $nids = array(1, 2, 3, 4, 5);

    // Add the condition to the existing query.
    $view-&gt;query-&gt;condition('nid', $nids, 'IN');
  }
}