bbp_pre_get_posts_normalize_forum_visibility( WP_Query $posts_query = null )

Adjusts forum, topic, and reply queries to exclude items that might be contained inside hidden or private forums that the user does not have the capability to view.

Description

Doing it with an action allows us to trap all WP_Query’s rather than needing to hardcode this logic into each query. It also protects forum content for plugins that might be doing their own queries.

Parameters

$posts_query

(Optional)

Default value: null

Return

(WP_Query)

Source

File: bp-forums/forums/functions.php

function bbp_pre_get_posts_normalize_forum_visibility( $posts_query = null ) {

	// Bail if all forums are explicitly allowed
	if ( true === apply_filters( 'bbp_include_all_forums', false, $posts_query ) ) {
		return;
	}

	// Bail if $posts_query is not an object or of incorrect class
	if ( !is_object( $posts_query ) || !is_a( $posts_query, 'WP_Query' ) ) {
		return;
	}

	// Get query post types array .
	$post_types = (array) $posts_query->get( 'post_type' );

	// Forums
	if ( bbp_get_forum_post_type() === implode( '', $post_types ) ) {

		// Prevent accidental wp-admin post_row override
		if ( is_admin() && isset( $_REQUEST['post_status'] ) ) {
			return;
		}

		/** Default ***********************************************************/

		// Get any existing post status
		$post_stati = $posts_query->get( 'post_status' );

		// Default to public status
		if ( empty( $post_stati ) ) {
			$post_stati = array( bbp_get_public_status_id() );

		// Split the status string
		} elseif ( is_string( $post_stati ) ) {
			$post_stati = explode( ',', $post_stati );
		}

		/** Private ***********************************************************/

		// Remove bbp_get_private_status_id() if user is not capable
		if ( ! current_user_can( 'read_private_forums' ) ) {
			$key = array_search( bbp_get_private_status_id(), $post_stati );
			if ( !empty( $key ) ) {
				unset( $post_stati[$key] );
			}

		// ...or add it if they are
		} else {
			$post_stati[] = bbp_get_private_status_id();
		}

		/** Hidden ************************************************************/

		// Remove bbp_get_hidden_status_id() if user is not capable
		if ( ! current_user_can( 'read_hidden_forums' ) ) {
			$key = array_search( bbp_get_hidden_status_id(), $post_stati );
			if ( !empty( $key ) ) {
				unset( $post_stati[$key] );
			}

		// ...or add it if they are
		} else {
			$post_stati[] = bbp_get_hidden_status_id();
		}

		// Add the statuses
		$posts_query->set( 'post_status', array_unique( array_filter( $post_stati ) ) );
	}

	// Topics Or Replies
	if ( array_intersect( array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), $post_types ) ) {

		// Get forums to exclude
		$forum_ids = bbp_exclude_forum_ids( 'meta_query' );

		// Bail if no forums to exclude
		if ( ! array_filter( $forum_ids ) ) {
			return;
		}

		// Get any existing meta queries
		$meta_query   = (array) $posts_query->get( 'meta_query', array() );

		// Add our meta query to existing
		$meta_query[] = $forum_ids;

		// Set the meta_query var
		$posts_query->set( 'meta_query', $meta_query );
	}
}

Changelog

Changelog
Version Description
bbPress (r3291) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.