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