So in the template I’ve multiple featured posts and sliders and 2 default loops.
I use get_posts
for everything except default loops (obviously).
Now the issue I’m having is excluding get_posts
posts from main loops. I could have gone query_posts
way but impact on performance is bad and I want to work on main loop even before its generated.
So I’m going to use pre_get_posts
in functions.php.
Inside pre_get_posts I’m going to modify main loop and exclude get_posts data.
I’ve following problems:
-
I need to use get_posts inside template, so I need to declare variable holding those as global 🙁 otherwise it will not be accessible inside my template.
-
get_posts inside pre_get_posts gives me memory issues: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 130968 bytes
function RoomsGazette_homepage_content( $query ) {
//$tweets = getTweets( 4, $titan->getOption( 'tweets_from_user' ) );
$header_sliders = get_posts( array( 'meta_key' => 'roomsgazette_hp_position', 'meta_value' => 1, 'posts_per_page' => 5, ) );
//$footer_sliders = get_posts( array( 'meta_key' => 'roomsgazette_hp_position', 'meta_value' => 2, 'posts_per_page' => 5, ) );
//$featured = get_posts( array( 'meta_key' => 'roomsgazette_hp_position', 'meta_value' => 3, 'posts_per_page' => 2, ) );
$exclude_ids = array();
foreach ( $header_sliders as $tmp ) {
array_push( $exclude_ids, $tmp->ID );
}
foreach ( $footer_sliders as $tmp ) {
array_push( $exclude_ids, $tmp->ID );
}
foreach ( $featured as $tmp ) {
array_push( $exclude_ids, $tmp->ID );
}
if ( $query->is_home() && $query->is_main_query() )
{
$query->set( 'post__not_in', $exclude_ids );
}
}
add_action( 'pre_get_posts', 'RoomsGazette_homepage_content' );