I'm trying to set a post__not_in
which use a global $popular
variable, defined in the index.php
.
-index.php-
$popular[] = 1 //post id=1
$popular[] = 2 //post id=2
$popular[] = 3 //post id=3
-functions.php-
function modify_query_exclude_popular($query) {
if ($query->is_main_query() && $query->is_home()){
global $popular;
$query->set('post__not_in', $popular);
}
}
add_action('pre_get_posts', 'modify_query_exclude_popular' );
I just can't make it work, the loop is working, but popular post_ids aren't exluded. I tried everything: define a constant, using "create_function" etc...
PS: If I set $popular
array inside the function modify_query_exclude_popular()
it does work, but I absolutely need to set the array in index.php
(the $popular
use post_id of another loop).