bbp_has_replies( mixed $args = '' )
The main reply loop. WordPress makes this easy for us
Description
Parameters
- $args
-
(Optional) All the arguments supported by WP_Query
Default value: ''
Return
(object) Multidimensional array of reply information
Source
File: bp-forums/replies/template.php
function bbp_has_replies( $args = '' ) {
global $wp_rewrite;
/** Defaults **************************************************************/
// Other defaults
$default_reply_search = !empty( $_REQUEST['rs'] ) ? $_REQUEST['rs'] : false;
$default_post_parent = ( bbp_is_single_topic() ) ? bbp_get_topic_id() : 'any';
$default_post_type = ( bbp_is_single_topic() && bbp_show_lead_topic() ) ? bbp_get_reply_post_type() : array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
$default_thread_replies = (bool) ( bbp_is_single_topic() && bbp_thread_replies() );
// Default query args
$default = array(
'post_type' => $default_post_type, // Only replies
'post_parent' => $default_post_parent, // Of this topic
'posts_per_page' => bbp_get_replies_per_page(), // This many
'paged' => bbp_get_paged(), // On this page
'orderby' => 'date', // Sorted by date
'order' => 'ASC', // Oldest to newest
'hierarchical' => $default_thread_replies, // Hierarchical replies
'ignore_sticky_posts' => true, // Stickies not supported
's' => $default_reply_search, // Maybe search
);
// What are the default allowed statuses (based on user caps)
if ( bbp_get_view_all() ) {
// Default view=all statuses
$post_statuses = array(
bbp_get_public_status_id(),
bbp_get_closed_status_id(),
bbp_get_spam_status_id(),
bbp_get_trash_status_id()
);
// Add support for private status
if ( current_user_can( 'read_private_replies' ) ) {
$post_statuses[] = bbp_get_private_status_id();
}
// Join post statuses together
$default['post_status'] = implode( ',', $post_statuses );
// Lean on the 'perm' query var value of 'readable' to provide statuses
} else {
$default['perm'] = 'readable';
}
/** Setup *****************************************************************/
// Parse arguments against default values
$r = bbp_parse_args( $args, $default, 'has_replies' );
// Set posts_per_page value if replies are threaded
$replies_per_page = $r['posts_per_page'];
if ( true === $r['hierarchical'] ) {
$r['posts_per_page'] = -1;
}
// Get Forums
$bbp = bbpress();
// Call the query
$bbp->reply_query = new WP_Query( $r );
// Add pagination values to query object
$bbp->reply_query->posts_per_page = $replies_per_page;
$bbp->reply_query->paged = $r['paged'];
// Never home, regardless of what parse_query says
$bbp->reply_query->is_home = false;
// Reset is_single if single topic
if ( bbp_is_single_topic() ) {
$bbp->reply_query->is_single = true;
}
// Only add reply to if query returned results
if ( (int) $bbp->reply_query->found_posts ) {
// Get reply to for each reply
foreach ( $bbp->reply_query->posts as &$post ) {
// Check for reply post type
if ( bbp_get_reply_post_type() === $post->post_type ) {
$reply_to = bbp_get_reply_to( $post->ID );
// Make sure it's a reply to a reply
if ( empty( $reply_to ) || ( bbp_get_reply_topic_id( $post->ID ) === $reply_to ) ) {
$reply_to = 0;
}
// Add reply_to to the post object so we can walk it later
$post->reply_to = $reply_to;
}
}
}
// Only add pagination if query returned results
if ( (int) $bbp->reply_query->found_posts && (int) $bbp->reply_query->posts_per_page ) {
// If pretty permalinks are enabled, make our pagination pretty
if ( $wp_rewrite->using_permalinks() ) {
// User's replies
if ( bbp_is_single_user_replies() ) {
$base = bbp_get_user_replies_created_url( bbp_get_displayed_user_id() );
// Root profile page
} elseif ( bbp_is_single_user() ) {
$base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
// Page or single post
} elseif ( is_page() || is_single() ) {
$base = get_permalink();
// Single topic
} else {
$base = get_permalink( bbp_get_topic_id() );
}
$base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
// Unpretty permalinks
} else {
$base = add_query_arg( 'paged', '%#%' );
}
// Figure out total pages
if ( true === $r['hierarchical'] ) {
$walker = new BBP_Walker_Reply;
$total_pages = ceil( (int) $walker->get_number_of_root_elements( $bbp->reply_query->posts ) / (int) $replies_per_page );
} else {
$total_pages = ceil( (int) $bbp->reply_query->found_posts / (int) $replies_per_page );
// Add pagination to query object
$bbp->reply_query->pagination_links = paginate_links( apply_filters( 'bbp_replies_pagination', array(
'base' => $base,
'format' => '',
'total' => $total_pages,
'current' => (int) $bbp->reply_query->paged,
'prev_text' => is_rtl() ? '→' : '←',
'next_text' => is_rtl() ? '←' : '→',
'mid_size' => 1,
'add_args' => ( bbp_get_view_all() ) ? array( 'view' => 'all' ) : false
) ) );
// Remove first page from pagination
if ( $wp_rewrite->using_permalinks() ) {
$bbp->reply_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->reply_query->pagination_links );
} else {
$bbp->reply_query->pagination_links = str_replace( '&paged=1', '', $bbp->reply_query->pagination_links );
}
}
}
// Return object
return apply_filters( 'bbp_has_replies', $bbp->reply_query->have_posts(), $bbp->reply_query );
}
Changelog
| Version | Description |
|---|---|
| bbPress (r2553) | 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.