bbp_parse_query( WP_Query $posts_query )

Add checks for bbPress conditions to parse_query action


Description Description

If it’s a user page, WP_Query::bbp_is_single_user is set to true.

If it’s a user edit page, WP_Query::bbp_is_single_user_edit is set to true and the the ‘wp-admin/includes/user.php’ file is included.

In addition, on user/user edit pages, WP_Query::home is set to false & query vars ‘bbp_user_id’ with the displayed user id is added.

In 2.6.0, the ‘author_name’ variable is no longer set when viewing a single user, because of is_author() weirdness. If this removal causes problems, it may come back in a future release.

If it’s a forum edit, WP_Query::bbp_is_forum_edit is set to true If it’s a topic edit, WP_Query::bbp_is_topic_edit is set to true If it’s a reply edit, WP_Query::bbp_is_reply_edit is set to true.

If it’s a view page, WP_Query::bbp_is_view is set to true If it’s a search page, WP_Query::bbp_is_search is set to true


Parameters Parameters

$posts_query

(Required)


Top ↑

Source Source

File: includes/core/template-functions.php

function bbp_parse_query( $posts_query ) {

	// Bail if $posts_query is not the main loop
	if ( ! $posts_query->is_main_query() ) {
		return;
	}

	// Bail if filters are suppressed on this query
	if ( true === $posts_query->get( 'suppress_filters' ) ) {
		return;
	}

	// Bail if in admin
	if ( is_admin() ) {
		return;
	}

	// Get query variables
	$bbp_view = $posts_query->get( bbp_get_view_rewrite_id() );
	$bbp_user = $posts_query->get( bbp_get_user_rewrite_id() );
	$is_edit  = $posts_query->get( bbp_get_edit_rewrite_id() );

	// It is a user page - We'll also check if it is user edit
	if ( ! empty( $bbp_user ) ) {

		/** Find User *********************************************************/

		// Setup the default user variable
		$the_user = false;

		// If using pretty permalinks, always use slug
		if ( get_option( 'permalink_structure' ) ) {
			$the_user = get_user_by( 'slug', $bbp_user );

		// If not using pretty permalinks, always use numeric ID
		} elseif ( is_numeric( $bbp_user ) ) {
			$the_user = get_user_by( 'id', $bbp_user );
		}

		// 404 and bail if user does not have a profile
		if ( empty( $the_user->ID ) || ! bbp_user_has_profile( $the_user->ID ) ) {
			$posts_query->bbp_is_404 = true;
			return;
		}

		/** User Exists *******************************************************/

		$is_favs        = $posts_query->get( bbp_get_user_favorites_rewrite_id()     );
		$is_subs        = $posts_query->get( bbp_get_user_subscriptions_rewrite_id() );
		$is_topics      = $posts_query->get( bbp_get_user_topics_rewrite_id()        );
		$is_replies     = $posts_query->get( bbp_get_user_replies_rewrite_id()       );
		$is_engagements = $posts_query->get( bbp_get_user_engagements_rewrite_id()   );

		// View or edit?
		if ( ! empty( $is_edit ) ) {

			// We are editing a profile
			$posts_query->bbp_is_single_user_edit = true;

			// Load the core WordPress contact methods
			if ( ! function_exists( '_wp_get_user_contactmethods' ) ) {
				require_once ABSPATH . 'wp-includes/registration.php';
			}

			// Load the edit_user functions
			if ( ! function_exists( 'edit_user' ) ) {
				require_once ABSPATH . 'wp-admin/includes/user.php';
			}

			// Load the grant/revoke super admin functions
			if ( is_multisite() && ! function_exists( 'revoke_super_admin' ) ) {
				require_once ABSPATH . 'wp-admin/includes/ms.php';
			}

			// Editing a user
			$posts_query->bbp_is_edit = true;

		// User favorites
		} elseif ( ! empty( $is_favs ) ) {
			$posts_query->bbp_is_single_user_favs = true;

		// User subscriptions
		} elseif ( ! empty( $is_subs ) ) {
			$posts_query->bbp_is_single_user_subs = true;

		// User topics
		} elseif ( ! empty( $is_topics ) ) {
			$posts_query->bbp_is_single_user_topics = true;

		// User topics
		} elseif ( ! empty( $is_replies ) ) {
			$posts_query->bbp_is_single_user_replies = true;

		// User engagements
		} elseif ( ! empty( $is_engagements ) ) {
			$posts_query->bbp_is_single_user_engagements = true;

		// User profile
		} else {
			$posts_query->bbp_is_single_user_profile = true;
		}

		// Make sure 404 is not set
		$posts_query->is_404  = false;

		// Correct is_home variable
		$posts_query->is_home = false;

		// Looking at a single user
		$posts_query->bbp_is_single_user = true;

		// User found so don't 404 yet
		$posts_query->bbp_is_404 = false;

		// User is looking at their own profile
		if ( bbp_get_current_user_id() === $the_user->ID ) {
			$posts_query->bbp_is_single_user_home = true;
		}

		// Set bbp_user_id for future reference
		$posts_query->set( 'bbp_user_id', $the_user->ID );

		// Set the displayed user global to this user
		bbpress()->displayed_user = $the_user;

	// View Page
	} elseif ( ! empty( $bbp_view ) ) {

		// Check if the view exists by checking if there are query args are set
		$view_args = bbp_get_view_query_args( $bbp_view );

		// Bail if view args are empty
		if ( empty( $view_args ) ) {
			$posts_query->bbp_is_404 = true;
			return;
		}

		// Correct is_home variable
		$posts_query->is_home     = false;

		// We are in a custom topic view
		$posts_query->bbp_is_view = true;

		// No 404 because views are all (currently) public
		$posts_query->bbp_is_404 = false;

	// Search Page
	} elseif ( isset( $posts_query->query_vars[ bbp_get_search_rewrite_id() ] ) ) {

		// Check if there are search query args set
		$search_terms = bbp_get_search_terms();
		if ( ! empty( $search_terms ) ) {
			$posts_query->bbp_search_terms = $search_terms;
		}

		// Correct is_home variable
		$posts_query->is_home = false;

		// We are in a search query
		$posts_query->bbp_is_search = true;

		// No 404 because search is always public
		$posts_query->bbp_is_404 = false;

	// Forum/Topic/Reply Edit Page
	} elseif ( ! empty( $is_edit ) ) {

		// Get the post type from the main query loop
		$post_type = $posts_query->get( 'post_type' );

		// Check which post_type we are editing, if any
		if ( ! empty( $post_type ) ) {
			switch ( $post_type ) {

				// We are editing a forum
				case bbp_get_forum_post_type() :
					$posts_query->bbp_is_forum_edit  = true;
					$posts_query->bbp_is_edit        = true;
					$posts_query->bbp_is_404         = false;
					break;

				// We are editing a topic
				case bbp_get_topic_post_type() :
					$posts_query->bbp_is_topic_edit  = true;
					$posts_query->bbp_is_edit        = true;
					$posts_query->bbp_is_404         = false;
					break;

				// We are editing a reply
				case bbp_get_reply_post_type() :
					$posts_query->bbp_is_reply_edit  = true;
					$posts_query->bbp_is_edit        = true;
					$posts_query->bbp_is_404         = false;
					break;
			}

		// We are editing a topic tag
		} elseif ( bbp_is_topic_tag() ) {
			$posts_query->bbp_is_topic_tag_edit = true;
			$posts_query->bbp_is_edit           = true;
			$posts_query->bbp_is_404            = false;
		}

		// We save post revisions on our own
		remove_action( 'pre_post_update', 'wp_save_post_revision' );

	// Topic tag page
	} elseif ( bbp_is_topic_tag() ) {
		$posts_query->set( 'bbp_topic_tag',  get_query_var( 'term' )   );
		$posts_query->set( 'post_type',      bbp_get_topic_post_type() );
		$posts_query->set( 'posts_per_page', bbp_get_topics_per_page() );

	// Do topics on forums root
	} elseif ( is_post_type_archive( bbp_get_post_types( array( 'has_archive' => true ) ) ) && ( 'topics' === bbp_show_on_root() ) ) {
		$posts_query->bbp_show_topics_on_root = true;
	}
}

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.