The obvious first step would be to use get_col
instead of get_result
. You will get an array returned straightaway so you could skip your foreach
and go straight to:
$args = array(
'author__in' => $following_users_ids,
'posts_per_page' => 12,
'paged' => $paged,
);
Behind the scenes, get_col
does much what you are doing but you are not going through the more complex get_results
function before doing it.
With thousands of users, I am not sure that that particular operation is going to be holdup that you think. That is, I’d suspect that maybe the delay is elsewhere, at least a significant part of the delay.
I’d suggest that:
- You make sure you table is indexed correctly
- And you cache the results— possibly cache both the IDs and the
post results