Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function hideFromUnknownUsers($postObject) {
- $rcUserLevel = get_post_meta ( $postObject->ID, 'rcUserLevel', true );
- if(have_posts())
- {
- if (!(
- !current_user_can ( 'read' ) &&
- (
- $rcUserLevel == 'Administrator' ||
- $rcUserLevel == 'Editor' ||
- $rcUserLevel == 'Author' ||
- $rcUserLevel == 'Contributor' ||
- $rcUserLevel == 'Subscriber'
- )
- )) {
- the_post();
- }
- }
- }
- add_action ( 'the_post', 'hideFromUnknownUsers' );
- function wpse225120_hide_posts_from_guests( $query ) {
- // If the user's logged in, exit the function now.
- if( is_user_logged_in() )
- return;
- // Get the current metadata query so we can alter it instead of overwriting it
- $meta_query = $query->get( 'meta_query' );
- // Create a meta-query filtering out hidden posts
- $hidden_meta_query = array(
- 'relation' => 'OR',
- array(
- 'key' => 'hide_from_guests',
- 'value' => 'false'
- ),
- array(
- 'key' => 'hide_from_guests',
- 'compare' => 'NOT_EXISTS'
- )
- );
- // If there's not already a meta-query, supply the one for hidden posts and exit
- if( ! is_array( $meta_query ) || empty( $meta_query ) ) {
- $query->set( 'meta_query', $hidden_meta_query );
- return;
- }
- // If there is an existing meta-query, modify it to support our new hidden posts
- // meta-query as a top-level 'AND' condition, if need be
- if( isset( $meta_query[ 'relation' ] ) && 'OR' === $meta_query[ 'relation' ] )
- $meta_query = array(
- 'relation' => 'AND',
- $meta_query
- );
- }
- // Add the hidden posts meta-query and overwrite $query's old meta-query
- $meta_query[] = $hidden_meta_query;
- $query->set( 'meta_query', $meta_query );
- }
- add_action( 'pre_get_posts', 'wpse225120_hide_posts_from_guests' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement