I have a filter that filters posts from the main loop depending on a user role (handled externally)
The meta data is entered when adding a post/page.
This is the filter - it works for filtering posts from the main page loops and the search pages but sticky posts are ignored and will show regardless of user role. Are these not included in the main_query?
add_filter( 'pre_get_posts', 'hide_private_posts' );
function hide_private_posts( $query ) {
$loggedInArray = array(
'key' => 'smartPrivate',
'value' => 'smartPrivate_loggedIn',
'compare' => '!='
);
$mentorArray = array(
'key' => 'smartPrivate',
'value' => 'smartPrivate_loggedInMentors',
'compare' => '!='
);
// If user isn't logged in, hide all private posts / pages
if( !is_user_logged_in() ) {
if ( $query->is_main_query() || $query->is_feed() ) {
$query->set( 'meta_query', array(
$loggedInArray,
$mentorArray
));
}
// If user is logged in but a learner, show logged in posts / pages
} else if( is_user_sso_role( array('learner') ) ) {
if ( $query->is_main_query() || $query->is_feed() ) {
$query->set( 'meta_query', array(
$mentorArray
));
}
}
return $query;
}