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%'”?
- Pagination with custom SQL query
- Whats the difference between post_limits and pre_get_posts?
- Using OR conditions in meta_query for query_posts argument
- Differences Between WP_Query() and get_posts() for Querying Posts?
- Should I use Transients with W3 Total Cache APC Caching? [closed]
- What is the most efficient way of querying posts based on visits and date for current day?
- Is it possible to wrap Geo Location search around WP_Query?
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Is it true $wpdb->get_results is faster than WP_Query in most cases?
- What kind of object type is WP_Query?
- How can I save an array from a random post sequence for later use?
- Is it possible to select against a post’s parent’s fields with WP_Query?
- Slow SQL_CALC_FOUND_ROWS Query
- Extending WP_Query — Optimise SQL query
- Getting an array out of WPQuery
- How to filter sql only for a specific post type
- get_the_title($postID) OR get_the_title()?
- How to make an activities stream mixing posts and comments?
- SQL Statement generated by WP_Query not producing expected results
- 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?
- Stop all SQL_CALC_FOUND_ROWS wordpress queries
- Recommended way to drop a pending query (in pre_get_posts)?
- how to retrieve specific product attribute value in an sql query?
- WP Meta Query for some meta (array) values
- meta_query with array as value
- Placing a div or img in between a post array using WP Query [closed]
- Slow speed and high CPU usage for my code. How to improve it?
- Multiple queries on Home Page – page load/site speed issue
- Show one post per author and limit query to 8 posts
- WP_Query Performance Issues with meta_query
- SQL query injection with fifu image
- Passing a SQL query to the WP Query
- Combine relationship posts with existing 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 – grouping posts by same meta key, adding together values from another key
- Retrieve all custom field values of a specific custom field metakey as an array, inside WP_Query
- Any way to use FETCH_KEY_PAIR with $wpdb?
- WP Query by 4 different taxonomies
- Trouble with serialized metadata
- What is the equivalent WP_Query of a SQL Query?
- What code to use in an array to call the current sub-category?
- WP_User_Query Orderby Not Working
- Loop through array of pages
- 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
- WP_Term_Query->get_terms() very slow query for WordPress filter
- Filtering posts for unique titles, only the most recent
- Repeat array inside array through while loop
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- 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
- What argument does my function need to echo get_results() query results
- 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)
- Website goes slow down after importing long database
- how to query for meta_value have array
- Optimising amount of calls to custom fields
- wp_query – Modify $query to include duplicate content
- Iterate through ID’s in loop
- 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
- WordPress WP_Query Array Custom search via taxonomies
- WP Query posts__in not working with array
- Custom WP_Query with SQL directives for getting posts by authors and terms
- Post OrderBy slow performance
- How to return value from sql and display it
- orderby in WP_query doesn’t works
- Sort Posts with custom meta key by default which is currently set as optional
- Dynamically name array in WordPress loop – add/get values
- Dynamically create array from page title
- WP_Query with several meta_query-statements and order by meta_value
- WordPress Query Returning Every Post
- Creating Custom Query
- wpdb LIKE request shows all database data
- Convert a WP Query into a simpler SQL query to fetch only COUNT of posts
- How do I have multiple metaqueries inside one wordpress query
- I want to place a post before all others from an ACF boleen field
- Add custom argument to WP_Query and modify SQL where clause
- Group By query based on Custom Field
- wp_query, calculate with two dates when ‘key’ is text format
- How to make OR condition in WP_Query
- Orderby meta _value that is saved as an array
- How to short circuit a wordpress query that is not related to posts?
- Query for posts belonging to multiple categories
- WP_Query equivalent of SQL UNION ALL