bbp_has_topics( array $args = array() )
The main topic loop. WordPress makes this easy for us
Description Description
Parameters Parameters
- $args
-
(Optional) All the arguments supported by WP_Query
Default value: array()
Return Return
(object) Multidimensional array of topic information
Source Source
File: includes/topics/template.php
function bbp_has_topics( $args = array() ) {
/** Defaults **************************************************************/
// Other defaults
$default_topic_search = bbp_sanitize_search_request( 'ts' );
$default_show_stickies = (bool) ( bbp_is_single_forum() || bbp_is_topic_archive() ) && ( false === $default_topic_search );
$default_post_parent = bbp_is_single_forum() ? bbp_get_forum_id() : 'any';
// Default argument array
$default = array(
'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics
'post_parent' => $default_post_parent, // Forum ID
'meta_key' => '_bbp_last_active_time', // Make sure topic has some last activity time
'meta_type' => 'DATETIME',
'orderby' => 'meta_value', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
'order' => 'DESC', // 'ASC', 'DESC'
'posts_per_page' => bbp_get_topics_per_page(), // Topics per page
'paged' => bbp_get_paged(), // Page Number
'show_stickies' => $default_show_stickies, // Ignore sticky topics?
'max_num_pages' => false, // Maximum number of pages to show
// Conditionally prime the cache for related posts
'update_post_family_cache' => true
);
// Only add 's' arg if searching for topics
// See https://bbpress.trac.wordpress.org/ticket/2607
if ( ! empty( $default_topic_search ) ) {
$default['s'] = $default_topic_search;
}
// What are the default allowed statuses (based on user caps)
if ( bbp_get_view_all( 'edit_others_topics' ) ) {
// Default view=all statuses
$post_statuses = array_keys( bbp_get_topic_statuses() );
// Add support for private status
if ( current_user_can( 'read_private_topics' ) ) {
$post_statuses[] = bbp_get_private_status_id();
}
// Join post statuses together
$default['post_status'] = $post_statuses;
// Lean on the 'perm' query var value of 'readable' to provide statuses
} else {
$default['perm'] = 'readable';
}
// Maybe query for topic tags
if ( bbp_is_topic_tag() ) {
$default['term'] = bbp_get_topic_tag_slug();
$default['taxonomy'] = bbp_get_topic_tag_tax_id();
}
/** Setup *****************************************************************/
// Parse arguments against default values
$r = bbp_parse_args( $args, $default, 'has_topics' );
// Get bbPress
$bbp = bbpress();
// Call the query
$bbp->topic_query = new WP_Query( $r );
// Maybe prime last active posts
if ( ! empty( $r['update_post_family_cache'] ) ) {
bbp_update_post_family_caches( $bbp->topic_query->posts );
}
// Set post_parent back to 0 if originally set to 'any'
if ( 'any' === $r['post_parent'] ) {
$r['post_parent'] = 0;
}
// Limited the number of pages shown
if ( ! empty( $r['max_num_pages'] ) ) {
$bbp->topic_query->max_num_pages = (int) $r['max_num_pages'];
}
/** Stickies **************************************************************/
// Put sticky posts at the top of the posts array
if ( ! empty( $r['show_stickies'] ) && ( $r['paged'] <= 1 ) ) {
bbp_add_sticky_topics( $bbp->topic_query, $r );
}
// If no limit to posts per page, set it to the current post_count
if ( -1 === $r['posts_per_page'] ) {
$r['posts_per_page'] = $bbp->topic_query->post_count;
}
// Add pagination values to query object
$bbp->topic_query->posts_per_page = (int) $r['posts_per_page'];
$bbp->topic_query->paged = (int) $r['paged'];
// Only add pagination if query returned results
if ( ( ! empty( $bbp->topic_query->post_count ) || ! empty( $bbp->topic_query->found_posts ) ) && ! empty( $bbp->topic_query->posts_per_page ) ) {
// Limit the number of topics shown based on maximum allowed pages
if ( ( ! empty( $r['max_num_pages'] ) ) && ( $bbp->topic_query->found_posts > ( $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count ) ) ) {
$bbp->topic_query->found_posts = $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count;
}
// Total topics for pagination boundaries
$total_pages = ( $bbp->topic_query->posts_per_page === $bbp->topic_query->found_posts )
? 1
: ceil( $bbp->topic_query->found_posts / $bbp->topic_query->posts_per_page );
// Maybe add view-all args
$add_args = bbp_get_view_all()
? array( 'view' => 'all' )
: false;
// Pagination settings with filter
$bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array(
'base' => bbp_get_topics_pagination_base( $r['post_parent'] ),
'format' => '',
'total' => $total_pages,
'current' => $bbp->topic_query->paged,
'prev_text' => is_rtl() ? '→' : '←',
'next_text' => is_rtl() ? '←' : '→',
'mid_size' => 1,
'add_args' => $add_args,
) );
// Add pagination to query object
$bbp->topic_query->pagination_links = bbp_paginate_links( $bbp_topic_pagination );
}
// Filter & return
return apply_filters( 'bbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query );
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |