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
- get custom post type by tag
- Order Search Results Page by meta_value If no Value Return Remaining Results
- Query WooCommerce orders where meta data does not exist
- author.php with ACF and CPTs
- Query with pre_get_posts to get pagination
- Trying to list out child pages with WP_Query
- view queries made?
- Exclude first 5 posts of specific categories in the main loop
- Pass the same object to multiple widgets in a template with one query
- WP_Tax_Query with post_tag not working
- Posts loop with pagination on a single post page
- WP_Query on custom field and order results
- how to merge a WPQuery array with a PHP array and use the Loop to Output the result
- Where is the WP_Query instance instantiated (when working with themes)?
- Iterating through $wpdb query without using get_results for large query results
- Show post number in widget loop
- Loop Performance > JSON files vs. WP_Query vs. REST API
- Include data from custom table in WP_Query
- WordPress ignoring LIMIT / posts_per_page despite being in wp_query
- Conflict with get_posts and the_content
- How to get a query variable?
- How to provide meta_key array to wp_query?
- How to add div blocks after certain set of post
- How to set up hierarchical relationships without using plugins / meta query
- Meta query compare for ID’s greater than specific ID
- wordpress udpate query not equal to
- WP_Query with one category in args shows other categories
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Why does apply_filters behave different inside and outside a loop?
- Custom template for password protected page
- Elementor posts custom query based on tag [closed]
- How can I do a orderby by the number of items? So basically list by starting with the array with the largest number of post
- pre_get_posts pagination not working
- Prevent Duplicate Post Counted by Query
- meta_query is overriding default search
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- Pagination inside the blog page not working
- Custom Query Pagination not working on static front page
- Is my code correct?
- why doesn’t this pre_get_posts code work?
- pre_get_posts with WP_Query to prevent posts from specific tags
- How can I modify standard search query to include also ACF custom fields values?
- Retrieving category pages from subcategory returns empty sets
- How to get query results for the next page
- Is it possible to add query parameters on the archive page?
- Different number of posts showing in development vs production server
- Multiple loop for “featured” items returns wrong posts
- Get WP_Query query after execution?
- Pagination in custom query not working [duplicate]
- How to write a query-function as a query-shortcode?
- Search results stuck on page 1