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?
- Should I use Transients with W3 Total Cache APC Caching? [closed]
- Is it true $wpdb->get_results is faster than WP_Query in most cases?
- Extending WP_Query — Optimise SQL query
- How to filter sql only for a specific post type
- How to make an activities stream mixing posts and comments?
- assign 2 $args to one wp_query
- SQL query equivalent to WP User Query
- WP Meta Query for some meta (array) values
- meta_query with array as value
- Show one post per author and limit query to 8 posts
- Filter WordPress posts by between parameter
- SELECT * FROM $wpdb->posts WHERE ID > 160
- How add a custom posttype name using ACF field to a query post array
- Too many posts cause slow load when paginating
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- Query post with meta_query where date is not in future
- WP Query for variable taxonomies
- WP_Query times out
- Improve wp query perfomance on large db
- $wpdb select date range of posts
- reducing the amount of wp_query calls
- How To Fix WP Query Returns Results But Shouldn’t?
- List of the years with posts presented
- Adding an array from a query string to a WP meta_query
- Query that loads a custom type AND posts whose ids are not inside those custom types custom fields
- Optional Meta Query
- 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
- How to increase load time of an archive/search page (WP_Query)
- Query all posts if multiple meta key don’t exist
- Get posts by list of post IDs ordered by those IDs?
- Which is faster wpdb & get_row or wp_query & ge_post_meta?
- How to pass orderby params to $wpdb->prepare()?
- Removing duplicate values between two wordpress queries
- Include current post into loop
- Is it possible to query from external database? [duplicate]
- Include post content of linked posts in search
- Make a SQL query with wpdb in WordPress
- Combine results of multiple WP_Query to resemble single WP_Query
- How do I turn these values from MYSQL into an array
- What is the best way (regarding performance) to set transients for logged in users?
- List of ways to access WordPress database?
- search serialised meta_value for date value?
- Combine 2 arrays in a query parameter
- Getting rid of unwanted nonSQL syntax characters when debugging a query
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- Slow Query On Search
- How do I add a relation parameter to my filter query?
- construct complex queries with WP User Query
- Get a list of ACF Repeater-Fields as array
- How to get specific string in explode array?
- How to SQL query posts IDs by categories AND authors?
- Remove from array in WP_Query loop
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- SQL query injection with fifu image
- Combine relationship posts with existing wp_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
- What is the equivalent WP_Query of a SQL Query?
- 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
- WP_Term_Query->get_terms() very slow query for WordPress filter
- Filtering posts for unique titles, only the most recent
- 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
- date_query problem
- What argument does my function need to echo get_results() query results
- WP_Query: Meta_Query with serialized value (or a workaround)
- Website goes slow down after importing long database
- how to query for meta_value have array
- Iterate through ID’s in loop
- Issue with WP_Query (need a array of selected ID’s)
- WordPress WP_Query Array Custom search via taxonomies
- Custom WP_Query with SQL directives for getting posts by authors and terms
- Post OrderBy slow performance
- orderby in WP_query doesn’t works
- Dynamically name array in WordPress loop – add/get values
- Dynamically create array from page title
- WordPress Query Returning Every Post
- Creating Custom Query
- 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
- wp_query, calculate with two dates when ‘key’ is text format