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
Related Posts:
- Calculating efficiently on large amount of data generated by wp_query
- WP_Query with “post_title LIKE ‘something%'”?
- Whats the difference between post_limits and pre_get_posts?
- Is it possible to wrap Geo Location search around WP_Query?
- Is it true $wpdb->get_results is faster than WP_Query in most cases?
- What kind of object type is WP_Query?
- Is it possible to select against a post’s parent’s fields with WP_Query?
- Getting an array out of WPQuery
- How to make an activities stream mixing posts and comments?
- What is an efficient way to query based on post_meta?
- assign 2 $args to one wp_query
- SQL query equivalent to WP User Query
- Use Transient API to cache queries for all posts in all categories?
- Placing a div or img in between a post array using WP Query [closed]
- Show one post per author and limit query to 8 posts
- WP_Query Performance Issues with meta_query
- Advanced WP Query hogs the SQL server
- SELECT * FROM $wpdb->posts WHERE ID > 160
- Too many posts cause slow load when paginating
- How can I pick a single post from the latest 3?
- Query post with meta_query where date is not in future
- WP_Query times out
- Improve wp query perfomance on large db
- reducing the amount of wp_query calls
- How To Fix WP Query Returns Results But Shouldn’t?
- List of the years with posts presented
- SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)
- Slow WP_Query for custom post type
- Adding an array from a query string to a WP meta_query
- Complex WP SQL Query
- Optional Meta Query
- Sorting events by descending date, and ascending time if multiple events on a date
- Editing the default wordpress search
- Weird orderby => post__in issue
- Slow page loads due to WordPress Core Query
- Display tags with random thumbnail from selection of posts with that tag
- How to extract specific post
- Woocommerce – Changing the order of the upsell products [closed]
- Changing sort order for presentation by Jetpack infinite scroll
- Loop Performance > JSON files vs. WP_Query vs. REST API
- Include data from custom table in WP_Query
- wordpress ajax relationship query
- How to increase load time of an archive/search page (WP_Query)
- Query all posts if multiple meta key don’t exist
- How to pass orderby params to $wpdb->prepare()?
- Removing duplicate values between two wordpress queries
- Include current post into loop
- Debugging wp_query orderby for taxonomy
- Getting additional columns from sql
- Combine results of multiple WP_Query to resemble single WP_Query
- How do I turn these values from MYSQL into an array
- Does meta_value (array) work with ‘orderby’?
- Slow Query On Search
- WP_Query: apply an SQL function to meta fileld value
- How do I add a relation parameter to my filter query?
- WP_Query with page_ids in arguments without a result
- Related Posts Excluding Certain Categories
- Query using string from custom field as array value
- Multiple meta query from array
- Category based on post id
- Randomly select one of last 3 posts
- Which is from this queries is more faster
- meta_query returning excluded result
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- Passing a SQL query to the WP Query
- WP_Query() not working as expected
- Counting instances of words in the results of a post query
- sanitize_post() is not sanitizing Post Object
- WP Query by 4 different taxonomies
- Trouble with serialized metadata
- WP_User_Query Orderby Not Working
- Translating WP query into to SQL query
- Unusual high query of user meta data
- update_post_meta performance in a loop woocommerce
- update_post_meta performance in a loop woocommerce
- Repeat array inside array through while loop
- How to search CPTs in draft using get_page_by_title()
- Sorting Posts by Taxonomy thats not within the query’s $args
- Showing the correct number of posts_per_page when avoiding the use of post__not_in
- Related Post by Tags Code
- date_query problem
- Include post_status check within $wpdb query
- WP_Query: Meta_Query with serialized value (or a workaround)
- Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
- Optimising amount of calls to custom fields
- wp_query – Modify $query to include duplicate content
- How to get several fields from wp_query?
- How to sort a WP_Query array by post_name after an array_merge();
- Issue with WP_Query (need a array of selected ID’s)
- WP Query to order posts by multiple meta fields
- WP Query posts__in not working with array
- Sort Posts with custom meta key by default which is currently set as optional
- Dynamically create array from page title
- WP_Query with several meta_query-statements and order by meta_value
- WordPress Query Returning Every Post
- wpdb LIKE request shows all database data
- How do I have multiple metaqueries inside one wordpress query
- Add custom argument to WP_Query and modify SQL where clause
- Group By query based on Custom Field
- Orderby meta _value that is saved as an array