他のどの投稿タイプよりも上に1つの投稿タイプを表示する
デフォルトでは、SearchWPは、エンジン設定で定義された重みを使用して、関連性に基づいて結果を返します。
関連性の重みが最も高くなくても、特定の投稿タイプを最初に表示したい場合があります。
SearchWPが関連性を計算する方法をカスタマイズすることで、これを実現できます。返されたすべての商品に特別な「ボーナスウェイト」を付与し、結果のトップに表示させます。
このボーナスウェイトはすべての商品に適用されるため、商品の並び順は計算された関連性を引き続き尊重します。商品以外の結果は、ボーナスウェイトを受け取った商品の下に関連性によって並べ替えられます。
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 | |
| // Give Products extraordinary weight boost to ensure Products show up first. | |
| // @link https://searchwp.com/documentation/knowledge-base/post-type-first-top/ | |
| add_filter( 'searchwp\query\mods', function( $mods ) { | |
| $post_type = 'product'; // Post type name. | |
| $source = \SearchWP\Utils::get_post_type_source_name( $post_type ); | |
| $mod = new \SearchWP\Mod( $source ); | |
| $mod->relevance( function( $runtime ) use ( $source ) { | |
| global $wpdb; | |
| return $wpdb->prepare( | |
| "IF( {$runtime->get_foreign_alias()}.source = %s, '999999999999', '0' )", | |
| $source | |
| ); | |
| } ); | |
| $mods[] = $mod; | |
| return $mods; | |
| } ); |
注意 7行目の投稿タイプnameは、同じ結果を達成するために登録済みの任意の投稿タイプ名に変更できます。

