Firstly, you are invoking an infinite loop, which causes the memory exhaustion. To avoid it, put the following at the beginning of your function:
// avoid infinite loop
remove_action( 'pre_get_posts', __FUNCTION__ );
It makes sure the you are not hooking it into pre_get_posts
over and over again, re-initiating your get_posts()
call over and over again.
Secondly, make use of WP_Query
‘s – and subsequently get_posts
‘s – the fields
parameter, choosing ids
as value, which reduces the memory-/workload by getting only IDs.
Related Posts:
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- WP_Query vs get_posts
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- WP_Query: query posts by ids from array?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- How-to exclude terms from the main query the most performant way?
- Query Custom Meta Value with Increment
- “pre_get_posts” firing on every query
- Identify which loop you are hooking into; primary or secondary?
- WordPress Number of Posts Not Changing With posts_per_page
- Transient pagination not working properly
- Can not switch the queried post in pre_get_posts hook
- meta_query on a date range using an array of values
- How to change posts order on Category and Tag archives?
- Undefined WP_Query::has_posts()?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- if/else on custom query gives 200 OK when condition not met?
- Using WP_Query in “parse_query” or “pre_get_post” in Posts2Posts
- Show Sticky Post at the top but do not show again in the loop?
- Can I set my WP_Query to be a Main Query?
- Show scheduled posts in main loop but not in WP_Query?
- Order by empty custom field
- Check if loop has any categories?
- How to get list of posts from permalinks?
- Is_single() conditional tag returns null in query
- Transient api Caches confused
- Include current post into loop
- WP Query – order posts by meta field first and then order the rest
- pre_get_posts – Trying to get property of non-object warning
- Should I reset $wp_query?
- How to filter query loop block with a search string from the query parameters
- I am officially missing something about transient posts
- `offset` WP_Query argument dont work via `pre_get_posts`
- pre_user_query vs pre_get_posts
- Can I alter the main loop to ‘orderby’ a custom callback?
- WP_Query & Duplicate entries
- how to show more than 1 post into three columns query
- Using the same WP_Query for shop and widgets in WooCommerce shop
- Ordering Posts by parent category, name ascending
- WP_Query secondary query failing
- wp_query – Modify $query to include duplicate content
- Loop through multiple custom fields with increasing number
- meta_query dates from an array
- Custom post types loop on a page template
- Main loop querying current template’s info only in custom category archive pages, not my posts
- Pre_get_post on CPT archive page
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Search query alteration not working for meta values
- Multiple queries and pagination
- How to Order By Two Custom Fields?
- Display specific page (that is child) content on parent page
- Different Loop for tag pages?
- Child pages not affected by orderby
- Write WP Query that selects posts that are part of the same two categories
- Ordering posts by custom field and grouped by month
- Pagination for custom loop on custom page template is not displaying anything
- Does putting queries within loops cause an issue on WordPress?
- Filtering by multiple conditions in the loop
- WP_User_Query orderby meta_val_num
- Hiding carousel indicators if there’s only one post
- Query Taxonomy By Page Title
- WP_Query custom order and pagination
- How to access $wp_query object from array of posts given by get_posts?
- Function using get_posts() with tax_query not working when called from functions.php
- Display all attached image of every post of custom post type and link to original post
- 2 queries with counters
- Get posts with no tags?
- wp-query, pull children of parent page
- relation OR instead of AND – Filtered term ID’s in loop
- Writing less unnecessary code with WordPress
- How to SQL query posts IDs by categories AND authors?
- Does query_posts have an effect on get_the_category?
- Second loop pagination changes URL, not content
- Unable to paginate a custom page query
- How to change the default post type over the loop?
- Include one page/post into query which is already returning posts
- Insert images into wordpress post with a query
- Query Not working as expected
- Why doesn’t my WP Meta Query return any results?
- Compare “Main” post ID to ID inside wp_query loop
- Offset WP_Query by negative 1
- WP_Query orderby random do not repeat infinite scroll – one loop
- Paginate pages with dynamic query
- WP_Query loop doen’t work with my custom taxonomy
- Advise on Templates for Custom Queries
- Search.php – return number of results but cannot loop through
- How do I get the content of a custom instance of WP_Query?
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Creating ‘posts page’ loop based on the page itself
- Why does apply_filters behave different inside and outside a loop?
- WordPress Queries Not Working?
- WP_Query meta compare must include ALL array values
- Determine if ID is page or post and query the ID
- Custom template for password protected page
- How to apply pre_get_posts to a custom query?
- Start Query from 2nd Post without offset
- Filter subpages in while loop from WP Query object
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- Use have_posts() with array of post results retrieved by $wpdb->get_results