bbp_has_topics

The main topic loop.

Description

(object) bbp_has_topics( (string) $args = '' ); 

WordPress makes this easy for us

Returns (object)

Multidimensional array of topic information

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

    ;        


    Usage

    1. if ( !function_exists( 'bbp_has_topics' ) ) { 
    2. require_once '/includes/topics/template.php'; 
    3.  
    4. // The args. 
    5. $args = ''; 
    6.  
    7. // NOTICE! Understand what this does before running. 
    8. $result = bbp_has_topics($args); 
    9.  

    Defined (1)

    The function is defined in the following location(s).

    /includes/topics/template.php  
    1. function bbp_has_topics( $args = '' ) { 
    2. global $wp_rewrite; 
    3.  
    4. /** Defaults **************************************************************/ 
    5.  
    6. // Other defaults 
    7. $default_topic_search = !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : false; 
    8. $default_show_stickies = (bool) ( bbp_is_single_forum() || bbp_is_topic_archive() ) && ( false === $default_topic_search ); 
    9. $default_post_parent = bbp_is_single_forum() ? bbp_get_forum_id() : 'any'; 
    10.  
    11. // Default argument array 
    12. $default = array( 
    13. 'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics 
    14. 'post_parent' => $default_post_parent, // Forum ID 
    15. 'meta_key' => '_bbp_last_active_time', // Make sure topic has some last activity time 
    16. 'orderby' => 'meta_value', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',  
    17. 'order' => 'DESC', // 'ASC', 'DESC' 
    18. 'posts_per_page' => bbp_get_topics_per_page(), // Topics per page 
    19. 'paged' => bbp_get_paged(), // Page Number 
    20. 's' => $default_topic_search, // Topic Search 
    21. 'show_stickies' => $default_show_stickies, // Ignore sticky topics? 
    22. 'max_num_pages' => false, // Maximum number of pages to show 
    23. ); 
    24.  
    25. // What are the default allowed statuses (based on user caps) 
    26. if ( bbp_get_view_all() ) { 
    27.  
    28. // Default view=all statuses 
    29. $post_statuses = array( 
    30. bbp_get_public_status_id(),  
    31. bbp_get_closed_status_id(),  
    32. bbp_get_spam_status_id(),  
    33. bbp_get_trash_status_id() 
    34. ); 
    35.  
    36. // Add support for private status 
    37. if ( current_user_can( 'read_private_topics' ) ) { 
    38. $post_statuses[] = bbp_get_private_status_id(); 
    39.  
    40. // Join post statuses together 
    41. $default['post_status'] = implode( ', ', $post_statuses ); 
    42.  
    43. // Lean on the 'perm' query var value of 'readable' to provide statuses 
    44. } else { 
    45. $default['perm'] = 'readable'; 
    46.  
    47. // Maybe query for topic tags 
    48. if ( bbp_is_topic_tag() ) { 
    49. $default['term'] = bbp_get_topic_tag_slug(); 
    50. $default['taxonomy'] = bbp_get_topic_tag_tax_id(); 
    51.  
    52. /** Setup *****************************************************************/ 
    53.  
    54. // Parse arguments against default values 
    55. $r = bbp_parse_args( $args, $default, 'has_topics' ); 
    56.  
    57. // Get bbPress 
    58. $bbp = bbpress(); 
    59.  
    60. // Call the query 
    61. $bbp->topic_query = new WP_Query( $r ); 
    62.  
    63. // Set post_parent back to 0 if originally set to 'any' 
    64. if ( 'any' === $r['post_parent'] ) 
    65. $r['post_parent'] = 0; 
    66.  
    67. // Limited the number of pages shown 
    68. if ( !empty( $r['max_num_pages'] ) ) 
    69. $bbp->topic_query->max_num_pages = $r['max_num_pages']; 
    70.  
    71. /** Stickies **************************************************************/ 
    72.  
    73. // Put sticky posts at the top of the posts array 
    74. if ( !empty( $r['show_stickies'] ) && $r['paged'] <= 1 ) { 
    75.  
    76. // Get super stickies and stickies in this forum 
    77. $stickies = bbp_get_super_stickies(); 
    78.  
    79. // Get stickies for current forum 
    80. if ( !empty( $r['post_parent'] ) ) { 
    81. $stickies = array_merge( $stickies, bbp_get_stickies( $r['post_parent'] ) ); 
    82.  
    83. // Remove any duplicate stickies 
    84. $stickies = array_unique( $stickies ); 
    85.  
    86. // We have stickies 
    87. if ( is_array( $stickies ) && !empty( $stickies ) ) { 
    88.  
    89. // Start the offset at -1 so first sticky is at correct 0 offset 
    90. $sticky_offset = -1; 
    91.  
    92. // Loop over topics and relocate stickies to the front. 
    93. foreach ( $stickies as $sticky_index => $sticky_ID ) { 
    94.  
    95. // Get the post offset from the posts array 
    96. $post_offsets = wp_filter_object_list( $bbp->topic_query->posts, array( 'ID' => $sticky_ID ), 'OR', 'ID' ); 
    97.  
    98. // Continue if no post offsets 
    99. if ( empty( $post_offsets ) ) { 
    100. continue; 
    101.  
    102. // Loop over posts in current query and splice them into position 
    103. foreach ( array_keys( $post_offsets ) as $post_offset ) { 
    104. $sticky_offset++; 
    105.  
    106. $sticky = $bbp->topic_query->posts[$post_offset]; 
    107.  
    108. // Remove sticky from current position 
    109. array_splice( $bbp->topic_query->posts, $post_offset, 1 ); 
    110.  
    111. // Move to front, after other stickies 
    112. array_splice( $bbp->topic_query->posts, $sticky_offset, 0, array( $sticky ) ); 
    113.  
    114. // Cleanup 
    115. unset( $stickies[$sticky_index] ); 
    116. unset( $sticky ); 
    117.  
    118. // Cleanup 
    119. unset( $post_offsets ); 
    120.  
    121. // Cleanup 
    122. unset( $sticky_offset ); 
    123.  
    124. // If any posts have been excluded specifically, Ignore those that are sticky. 
    125. if ( !empty( $stickies ) && !empty( $r['post__not_in'] ) ) { 
    126. $stickies = array_diff( $stickies, $r['post__not_in'] ); 
    127.  
    128. // Fetch sticky posts that weren't in the query results 
    129. if ( !empty( $stickies ) ) { 
    130.  
    131. // Query to use in get_posts to get sticky posts 
    132. $sticky_query = array( 
    133. 'post_type' => bbp_get_topic_post_type(),  
    134. 'post_parent' => 'any',  
    135. 'meta_key' => '_bbp_last_active_time',  
    136. 'orderby' => 'meta_value',  
    137. 'order' => 'DESC',  
    138. 'include' => $stickies 
    139. ); 
    140.  
    141. // Cleanup 
    142. unset( $stickies ); 
    143.  
    144. // Conditionally exclude private/hidden forum ID's 
    145. $exclude_forum_ids = bbp_exclude_forum_ids( 'array' ); 
    146. if ( ! empty( $exclude_forum_ids ) ) { 
    147. $sticky_query['post_parent__not_in'] = $exclude_forum_ids; 
    148.  
    149. // What are the default allowed statuses (based on user caps) 
    150. if ( bbp_get_view_all() ) { 
    151. $sticky_query['post_status'] = $r['post_status']; 
    152.  
    153. // Lean on the 'perm' query var value of 'readable' to provide statuses 
    154. } else { 
    155. $sticky_query['post_status'] = $r['perm']; 
    156.  
    157. // Get all stickies 
    158. $sticky_posts = get_posts( $sticky_query ); 
    159. if ( !empty( $sticky_posts ) ) { 
    160.  
    161. // Get a count of the visible stickies 
    162. $sticky_count = count( $sticky_posts ); 
    163.  
    164. // Merge the stickies topics with the query topics . 
    165. $bbp->topic_query->posts = array_merge( $sticky_posts, $bbp->topic_query->posts ); 
    166.  
    167. // Adjust loop and counts for new sticky positions 
    168. $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count; 
    169. $bbp->topic_query->post_count = (int) $bbp->topic_query->post_count + (int) $sticky_count; 
    170.  
    171. // Cleanup 
    172. unset( $sticky_posts ); 
    173.  
    174. // If no limit to posts per page, set it to the current post_count 
    175. if ( -1 === $r['posts_per_page'] ) 
    176. $r['posts_per_page'] = $bbp->topic_query->post_count; 
    177.  
    178. // Add pagination values to query object 
    179. $bbp->topic_query->posts_per_page = $r['posts_per_page']; 
    180. $bbp->topic_query->paged = $r['paged']; 
    181.  
    182. // Only add pagination if query returned results 
    183. if ( ( (int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts ) && (int) $bbp->topic_query->posts_per_page ) { 
    184.  
    185. // Limit the number of topics shown based on maximum allowed pages 
    186. if ( ( !empty( $r['max_num_pages'] ) ) && $bbp->topic_query->found_posts > $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count ) 
    187. $bbp->topic_query->found_posts = $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count; 
    188.  
    189. // If pretty permalinks are enabled, make our pagination pretty 
    190. if ( $wp_rewrite->using_permalinks() ) { 
    191.  
    192. // User's topics 
    193. if ( bbp_is_single_user_topics() ) { 
    194. $base = bbp_get_user_topics_created_url( bbp_get_displayed_user_id() ); 
    195.  
    196. // User's favorites 
    197. } elseif ( bbp_is_favorites() ) { 
    198. $base = bbp_get_favorites_permalink( bbp_get_displayed_user_id() ); 
    199.  
    200. // User's subscriptions 
    201. } elseif ( bbp_is_subscriptions() ) { 
    202. $base = bbp_get_subscriptions_permalink( bbp_get_displayed_user_id() ); 
    203.  
    204. // Root profile page 
    205. } elseif ( bbp_is_single_user() ) { 
    206. $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() ); 
    207.  
    208. // View 
    209. } elseif ( bbp_is_single_view() ) { 
    210. $base = bbp_get_view_url(); 
    211.  
    212. // Topic tag 
    213. } elseif ( bbp_is_topic_tag() ) { 
    214. $base = bbp_get_topic_tag_link(); 
    215.  
    216. // Page or single post 
    217. } elseif ( is_page() || is_single() ) { 
    218. $base = get_permalink(); 
    219.  
    220. // Forum archive 
    221. } elseif ( bbp_is_forum_archive() ) { 
    222. $base = bbp_get_forums_url(); 
    223.  
    224. // Topic archive 
    225. } elseif ( bbp_is_topic_archive() ) { 
    226. $base = bbp_get_topics_url(); 
    227.  
    228. // Default 
    229. } else { 
    230. $base = get_permalink( (int) $r['post_parent'] ); 
    231.  
    232. // Use pagination base 
    233. $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' ); 
    234.  
    235. // Unpretty pagination 
    236. } else { 
    237. $base = add_query_arg( 'paged', '%#%' ); 
    238.  
    239. // Pagination settings with filter 
    240. $bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array ( 
    241. 'base' => $base,  
    242. 'format' => '',  
    243. 'total' => $r['posts_per_page'] === $bbp->topic_query->found_posts ? 1 : ceil( (int) $bbp->topic_query->found_posts / (int) $r['posts_per_page'] ),  
    244. 'current' => (int) $bbp->topic_query->paged,  
    245. 'prev_text' => is_rtl() ? '→' : '←',  
    246. 'next_text' => is_rtl() ? '←' : '→',  
    247. 'mid_size' => 1 
    248. ) ); 
    249.  
    250. // Add pagination to query object 
    251. $bbp->topic_query->pagination_links = paginate_links( $bbp_topic_pagination ); 
    252.  
    253. // Remove first page from pagination 
    254. $bbp->topic_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->topic_query->pagination_links ); 
    255.  
    256. // Return object 
    257. return apply_filters( 'bbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query );