検索結果をアクティブなbbPressフォーラムに限定する
一部のbbPressフォーラムのインストールは巨大です。おそらく、現在表示されているフォーラムにページ上の検索を制限することで、訪問者を支援したいと考えているでしょう。get_search_form()(WordPress検索(bbPressではない)ウィジェットが含まれます)の出力をカスタマイズして、検索時に表示されているbbPressフォーラムのみを考慮するようにすることができます。
All hooks should be added to your custom SearchWP Customizations Plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Customize native WordPress search form to include current bbPress Forum ID if applicable. | |
| // @link https://searchwp.com/documentation/knowledge-base/limiting-search-results-bbpress/ | |
| add_filter( 'get_search_form', function( $markup ) { | |
| if ( ! function_exists( 'bbp_get_forum_id' ) ) { | |
| return $markup; | |
| } | |
| $forum_id = isset( $_REQUEST['swpforumid'] ) ? absint( $_REQUEST['swpforumid'] ) : bbp_get_forum_id(); | |
| if ( empty( $forum_id ) ) { | |
| return $markup; | |
| } | |
| return str_replace( '</form>', | |
| '<input type="hidden" name="swpforumid" value="' . esc_attr( $forum_id ) . '" /></form>', | |
| $markup | |
| ); | |
| } ); | |
| // Limit SearchWP results to the current bbPress Forum when applicable. | |
| add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
| if ( ! isset( $_REQUEST['swpforumid'] ) ) { | |
| return $mods; | |
| } | |
| $mod = new \SearchWP\Mod( | |
| \SearchWP\Utils::get_post_type_source_name( 'post' ) | |
| ); | |
| $mod->set_where( [ [ | |
| 'column' => 'id', | |
| 'value' => absint( $_REQUEST['swpforumid'] ), | |
| 'compare' => '=', | |
| 'type' => 'NUMERIC', | |
| ] ] ); | |
| $mods[] = $mod; | |
| return $mods; | |
| }, 30, 2 ); |

