bbp_has_forums( mixed $args = '' )

The main forum loop.

Description

WordPress makes this easy for us.

Parameters

$args

(mixed) (Optional) All the arguments supported by WP_Query

Default value: ''

Return

(object) Multidimensional array of forum information

Source

File: bp-forums/forums/template.php

function bbp_has_forums( $args = '' ) {
	global $wp_rewrite;

	// Forum archive only shows root
	if ( bbp_is_forum_archive() ) {
		$default_post_parent = 0;

	// User subscriptions shows any
	} elseif ( bbp_is_subscriptions() ) {
		$default_post_parent = 'any';

	// Could be anything, so look for possible parent ID
	} else {
		$default_post_parent = bbp_get_forum_id();
	}

	// Parse arguments with default forum query for most circumstances
	$bbp_f = bbp_parse_args( $args, array(
		'post_type'           => bbp_get_forum_post_type(),
		'post_parent'         => $default_post_parent,
		'post_status'         => bbp_get_public_status_id(),
		'posts_per_page'      => bbp_get_forums_per_page(),
		'ignore_sticky_posts' => true,
		'orderby'             => 'menu_order title',
		'order'               => 'ASC',
		'paged'               => bbp_get_paged(),           // Page Number
	), 'has_forums' );

	// Run the query
	$bbp              = bbpress();
	$bbp->forum_query = new WP_Query( $bbp_f );

	// Add pagination values to query object
	$bbp->forum_query->posts_per_page = $bbp_f['posts_per_page'];
	$bbp->forum_query->paged          = $bbp_f['paged'];

	// Only add pagination if query returned results
	if ( ( (int) $bbp->forum_query->post_count || (int) $bbp->forum_query->found_posts ) && (int) $bbp->forum_query->posts_per_page ) {

		// Limit the number of forums shown based on maximum allowed pages
		if ( ( !empty( $bbp_f['max_num_pages'] ) ) && $bbp->forum_query->found_posts > $bbp->forum_query->max_num_pages * $bbp->forum_query->post_count )
			$bbp->forum_query->found_posts = $bbp->forum_query->max_num_pages * $bbp->forum_query->post_count;

		$base = $base = add_query_arg( 'paged', '%#%', bbp_get_forums_url() );

		// Pagination settings with filter
		$bbp_topic_pagination = apply_filters( 'bbp_forum_pagination', array (
			'base'      => $base,
			'format'    => '',
			'total'     => $bbp_f['posts_per_page'] === $bbp->forum_query->found_posts ? 1 : ceil( (int) $bbp->forum_query->found_posts / (int) $bbp_f['posts_per_page'] ),
			'current'   => (int) $bbp->forum_query->paged,
			'prev_text' => is_rtl() ? '→' : '←',
			'next_text' => is_rtl() ? '←' : '→',
			'mid_size'  => 1
		) );

		// Add pagination to query object
		$bbp->forum_query->pagination_links = paginate_links( $bbp_topic_pagination );

		// Remove first page from pagination
		$bbp->forum_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->forum_query->pagination_links );
	}

	return apply_filters( 'bbp_has_forums', $bbp->forum_query->have_posts(), $bbp->forum_query );
}

Changelog

Changelog
Version Description
bbPress (r2464) 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.