pre_get_posts with get_posts

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)