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 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 Parameters

$posts_query

(Optional)

Default value: null


Top ↑

Return Return

(WP_Query)


Top ↑

Source Source

File: includes/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 ***********************************************************/

		// Add all supported forum visibilities
		$posts_query->set( 'post_status', array_keys( bbp_get_forum_visibilities() ) );

		// Get forums to exclude
		$hidden_ids = bbp_exclude_forum_ids( 'array' );

		// Bail if no forums to exclude
		if ( empty( $hidden_ids ) ) {
			return;
		}

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

		// Add our meta query to existing
		$not_in = array_unique( array_merge( $not_in, $hidden_ids ) );

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

	// Some other post type besides Forums, Topics, or Replies
	} elseif ( ! array_diff( $post_types, bbp_get_post_types() ) ) {

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

		// Bail if no forums to exclude
		if ( empty( $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 );
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.