【bbpress】フォーラムページにトピック一覧が表示されないときの対処法
こんにちは、webエンジニアのゾノ( @ozonosho )です。
今回の記事ではwordpressの掲示板プラグイン「bbpress」においてフォーラムページにトピック一覧が表示されないときの対処法をご紹介します。
bbpressではごく稀なケースとして、トピックを正常に作成できたにも関わらずフォーラムページでトピック一覧が表示されず「トピックは見つかりませんでした!」と表示されることがあります。
僕はこれまでbbpressを利用した掲示板サイトを数多く作成してきましたが、今回はじめてこの現象が起きました。調べてみると、Wordpressのバージョンや使用しているテーマとの相性によって起こる現象のようです。
解決にかなり苦労したので、、、対処法を残しておきます!
bbpressで各フォーラムページにトピック一覧が表示されないときの対処法
先に答えを記載しちゃうと、下記のコードをプラグイン化して利用することで解決できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
<?php /* Plugin Name: bbpress-have-posts-repair */ add_filter ('bbp_has_replies', 'rw_bbp_has_replies') ; add_filter ('bbp_has_topics', 'rw_bbp_has_topics') ; function rw_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 ); //FIX to unset 's' if ($default['s'] == False) unset ($default['s']) ; // 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 bbPress $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( 'rw_bbp_has_replies', $bbp->reply_query->have_posts(), $bbp->reply_query ); } function rw_bbp_has_topics( $args = '' ) { global $wp_rewrite; /** Defaults **************************************************************/ // Other defaults $default_topic_search = !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : false; $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 '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 's' => $default_topic_search, // Topic Search 'show_stickies' => $default_show_stickies, // Ignore sticky topics? 'max_num_pages' => false, // Maximum number of pages to show ); //FIX to unset 's' if ($default['s'] == False) unset ($default['s']) ; // 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_topics' ) ) { $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'; } // 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 ); // 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 = $r['max_num_pages']; /** Stickies **************************************************************/ // Put sticky posts at the top of the posts array if ( !empty( $r['show_stickies'] ) && $r['paged'] <= 1 ) { // Get super stickies and stickies in this forum $stickies = bbp_get_super_stickies(); // Get stickies for current forum if ( !empty( $r['post_parent'] ) ) { $stickies = array_merge( $stickies, bbp_get_stickies( $r['post_parent'] ) ); } // Remove any duplicate stickies $stickies = array_unique( $stickies ); // We have stickies if ( is_array( $stickies ) && !empty( $stickies ) ) { // Start the offset at -1 so first sticky is at correct 0 offset $sticky_offset = -1; // Loop over topics and relocate stickies to the front. foreach ( $stickies as $sticky_index => $sticky_ID ) { // Get the post offset from the posts array $post_offsets = wp_filter_object_list( $bbp->topic_query->posts, array( 'ID' => $sticky_ID ), 'OR', 'ID' ); // Continue if no post offsets if ( empty( $post_offsets ) ) { continue; } // Loop over posts in current query and splice them into position foreach ( array_keys( $post_offsets ) as $post_offset ) { $sticky_offset++; $sticky = $bbp->topic_query->posts[$post_offset]; // Remove sticky from current position array_splice( $bbp->topic_query->posts, $post_offset, 1 ); // Move to front, after other stickies array_splice( $bbp->topic_query->posts, $sticky_offset, 0, array( $sticky ) ); // Cleanup unset( $stickies[$sticky_index] ); unset( $sticky ); } // Cleanup unset( $post_offsets ); } // Cleanup unset( $sticky_offset ); // If any posts have been excluded specifically, Ignore those that are sticky. if ( !empty( $stickies ) && !empty( $r['post__not_in'] ) ) { $stickies = array_diff( $stickies, $r['post__not_in'] ); } // Fetch sticky posts that weren't in the query results if ( !empty( $stickies ) ) { // Query to use in get_posts to get sticky posts $sticky_query = array( 'post_type' => bbp_get_topic_post_type(), 'post_parent' => 'any', 'meta_key' => '_bbp_last_active_time', 'orderby' => 'meta_value', 'order' => 'DESC', 'include' => $stickies ); // Cleanup unset( $stickies ); // Conditionally exclude private/hidden forum ID's $exclude_forum_ids = bbp_exclude_forum_ids( 'array' ); if ( ! empty( $exclude_forum_ids ) ) { $sticky_query['post_parent__not_in'] = $exclude_forum_ids; } // What are the default allowed statuses (based on user caps) if ( bbp_get_view_all() ) { $sticky_query['post_status'] = $r['post_status']; // Lean on the 'perm' query var value of 'readable' to provide statuses } else { $sticky_query['post_status'] = $r['perm']; } // Get all stickies $sticky_posts = get_posts( $sticky_query ); if ( !empty( $sticky_posts ) ) { // Get a count of the visible stickies $sticky_count = count( $sticky_posts ); // Merge the stickies topics with the query topics . $bbp->topic_query->posts = array_merge( $sticky_posts, $bbp->topic_query->posts ); // Adjust loop and counts for new sticky positions $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count; $bbp->topic_query->post_count = (int) $bbp->topic_query->post_count + (int) $sticky_count; // Cleanup unset( $sticky_posts ); } } } } // 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 = $r['posts_per_page']; $bbp->topic_query->paged = $r['paged']; // Only add pagination if query returned results if ( ( (int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts ) && (int) $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; // If pretty permalinks are enabled, make our pagination pretty if ( $wp_rewrite->using_permalinks() ) { // User's topics if ( bbp_is_single_user_topics() ) { $base = bbp_get_user_topics_created_url( bbp_get_displayed_user_id() ); // User's favorites } elseif ( bbp_is_favorites() ) { $base = bbp_get_favorites_permalink( bbp_get_displayed_user_id() ); // User's subscriptions } elseif ( bbp_is_subscriptions() ) { $base = bbp_get_subscriptions_permalink( bbp_get_displayed_user_id() ); // Root profile page } elseif ( bbp_is_single_user() ) { $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() ); // View } elseif ( bbp_is_single_view() ) { $base = bbp_get_view_url(); // Topic tag } elseif ( bbp_is_topic_tag() ) { $base = bbp_get_topic_tag_link(); // Page or single post } elseif ( is_page() || is_single() ) { $base = get_permalink(); // Forum archive } elseif ( bbp_is_forum_archive() ) { $base = bbp_get_forums_url(); // Topic archive } elseif ( bbp_is_topic_archive() ) { $base = bbp_get_topics_url(); // Default } else { $base = get_permalink( (int) $r['post_parent'] ); } // Use pagination base $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' ); // Unpretty pagination } else { $base = add_query_arg( 'paged', '%#%' ); } // Pagination settings with filter $bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array ( 'base' => $base, 'format' => '', 'total' => $r['posts_per_page'] === $bbp->topic_query->found_posts ? 1 : ceil( (int) $bbp->topic_query->found_posts / (int) $r['posts_per_page'] ), 'current' => (int) $bbp->topic_query->paged, 'prev_text' => is_rtl() ? '→' : '←', 'next_text' => is_rtl() ? '←' : '→', 'mid_size' => 1 ) ); // Add pagination to query object $bbp->topic_query->pagination_links = paginate_links( $bbp_topic_pagination ); // Remove first page from pagination $bbp->topic_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->topic_query->pagination_links ); } // Return object return apply_filters( 'rw_bbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query ); } ?> |
上記コードをコピーした後、適当な名前をつけたphpファイル(ここでは「bbpress-have-posts-repair.php」とする)を作成して貼り付けてください。
そして「bbpress-have-posts-repair」と名付けたフォルダを作成し、このphpファイルをフォルダ内にいれます。プラグインはこれで完成です。
続いてWordpressプラグインフォルダに「bbpress-have-posts-repair」フォルダをアップロードしてください。
するとWordpressの管理画面内にあるプラグインページに作成したプラグインが表示されているので、このプラグインを有効化すれば完了です。
これでフォーラムページにトピック一覧が表示されるはずです。
結局何が原因だったの?
今回の現象はフォーラムに紐づいたトピック一覧を取得するためのbbpress関数「bbp_has_topics()」がうまく機能しないために起きていました。
特定のバージョンやテーマを使用している場合のみ起こるようで、解決方法を探していたら僕の他にも数件の報告がありました。
ちなみに僕は今回「Wordpressバージョン:5.0.3」「使用テーマ:STORK」という環境でこの不具合が発生したので、もしかしたらSTORKとbbpressは相性が悪いのかもしれません。
おわりに
以上、今回の記事ではwordpressの掲示板プラグイン「bbpress」においてフォーラムページにトピック一覧が表示されないときの対処法を紹介させていただきました。
当サイトではこの記事の他にもbbPressのカスタマイズ方法を多数紹介しているのでぜひご覧ください。
また、ご自身で制作するのが難しい方に向けて格安のオーダー制作も受付しています。
- 5万円からご依頼可能!掲示板オーダー制作受付中。
-
WordPressの掲示板用プラグイン「bbPress」をカスタマイズして、使いやすい掲示板を5万円~の超低価格でオーダー制作いたします。あなたが本来注力すべきサイト運営に専念できるよう、掲示板導入のプロにお任せください。
掲示板オーダー制作の詳細はこちら