\SearchWP\Source
\SearchWP\Source は、SearchWP のインデックス作成および検索プロセスにおけるコンテンツタイプをモデル化するために設計された抽象クラスです。\SearchWP\Source は エンジン に追加され、それが \SearchWP\Query をインスタンス化する際に使用されます。
SearchWP には、デフォルトで利用可能な多数のコア \SearchWP\Source があります。それらには以下が含まれます:
これらの \SearchWP\Source はデフォルトで利用可能であるため、エンジン を設定する際にすぐに使用できます。
基本的な使い方
このクラスと searchwp\sources フックを使用して、カスタムデータベーステーブルを含むカスタムコンテンツタイプに一致する独自の \SearchWP\Source を構築できます。
| <?php | |
| // @link https://searchwp.com/documentation/classes/searchwp-source/ | |
| /** | |
| * Adds custom SearchWP\Source for MyCustomSearchWPSource which uses a custom database table. | |
| */ | |
| add_action( 'plugins_loaded', function() { | |
| class MyCustomSearchWPSource extends \SearchWP\Source { | |
| // Unique name for this Source. | |
| protected $name = 'mycustomsource'; | |
| // Database column name that provides a unique ID for each entry of the Source. | |
| protected $db_id_column = 'id'; | |
| function __construct() { | |
| global $wpdb; | |
| // The database table storing entries. | |
| $this->db_table = $wpdb->get_blog_prefix() . 'my_db_table'; | |
| // Labels for this Source. | |
| $this->labels = [ | |
| 'plural' => 'My Custom Source Entries', | |
| 'singular' => 'My Custom Source Entry', | |
| ]; | |
| // Attributes for this Source. Array of attributes, can be expanded. | |
| $this->attributes = [ [ | |
| 'name' => 'content', | |
| 'label' => 'Entry Content', | |
| 'default' => \SearchWP\Utils::get_min_engine_weight(), | |
| 'data' => function( $entry_id ) { | |
| // Note: MyWidget is an pseudo-class meant to represent | |
| // a class you're using that represents something like | |
| // WP_Post in that it models the data you are working with. | |
| // Long story short: this method should return the content | |
| // you want to index and make searchable in SearchWP. | |
| return MyWidget::get_content( $entry_id ); | |
| }, | |
| ], ]; | |
| } | |
| // Returns a native object e.g. formatted in a way you expect. | |
| public function entry( \SearchWP\Entry $entry, $query = false ) { | |
| // Note: MyWidget is a pseudo-class meant to represent a class | |
| // you're using that represents something like WP_Post in that it | |
| // models the data you're working with. | |
| // Long story short: this method should return whatever it is you | |
| // expect when working 'natively' with your data e.g. for WordPress | |
| // pages a WP_Post is returned. Note that $entry->get_id() returns | |
| // the value in the $db_id_column for this Source. | |
| return new MyWidget( $entry->get_id() ); | |
| } | |
| } | |
| // Append this Source to the list of available Sources in SearchWP. | |
| add_filter( 'searchwp\sources', function( $sources ) { | |
| $sources[] = new MyCustomSearchWPSource(); | |
| return $sources; | |
| } ); | |
| } ); |
注意: 登録済みの任意の \SearchWP\Source は、searchwp\sources フックを使用して削除することもできます。
引数
新しい \SearchWP\Source をインスタンス化する際に引数はありません。
プロパティ
\SearchWP\Source を拡張する際には、考慮すべきプロパティがいくつかあります。
name(string)- 一意の名前。(デフォルト:
'') labels(array)- ラベル。
singularとpluralのキーを持つ配列。(デフォルト:[]) db_table(string)- インデックス作成されるエントリを格納するために使用されるデータベーステーブル。(デフォルト:
'') db_id_column(string)- エントリ ID を追跡するために使用されるデータベース列。(デフォルト:
'') attributes(array)- エンジンでこのソースを設定する際に、関連性の重みを受け取る個々の
属性。(デフォルト:[]) - 各キーが以下を持つ、
属性または属性設定配列の配列: 'name'(string) 属性名。'label'(string) 属性ラベル。'default'(integer) デフォルトの重み(デフォルトとして省略するにはゼロ)。'options'(array) オプション。この属性の個々のインスタンスを定義します。'data'(mixed) エントリをインスタンス化する際のこの属性のデータを定義します。- – 最初のパラメータは
$db_id_columnに従ったエントリ ID です - – 2 番目のパラメータ(該当する場合)は、
optionsから選択されたoptionです rules(array)- 検索結果としてのエントリの候補を制御する個々の
ルール。(デフォルト:[]) - ルールまたはルール設定の配列。それぞれ以下のキーを持ちます。
'name'(string) ルール名。'label'(string) ルールのラベル。'options'(array) オプション。このルールの個々のオプションを定義します。'conditions'(array) オプション。このルールの個々の条件を定義します。'values'(array) オプション。このルールの各オプションの値を定義します。'application'(mixed) ルールロジックを適用します。- – 最初のパラメータには、配列としてのルールのプロパティが含まれます
option選択されたオプションcondition選択された条件value選択された値
メソッド
\SearchWP\Source を拡張する場合、考慮すべきメソッドがいくつかあります。
db_where(arrayを返します)- インデックス作成と検索の両方に満たされる必要がある
WHERE句の配列。(デフォルト:[]) 'column'(string) データベースカラム'value'(mixed) 値'type'(string) 値のタイプ。'CHAR'または'NUMERIC'のいずれかになります'compare'(string)valueを比較する方法- 利用可能な比較タイプには、
'='、'!='、'>'、'>='、'<'、'<='、'LIKE'、'NOT LIKE'、'IN'、'NOT IN'、'BETWEEN'、'NOT BETWEEN'、'EXISTS'、'NOT EXISTS'が含まれます entry(mixedを返します)- 検索結果が見つかった場合、このメソッドは結果のネイティブ表現を取得するために呼び出されます。(デフォルト:
stdClass) - – 最初のパラメータは、見つかった
\SearchWP\Entryです - – 2番目のパラメータは、この結果を見つけた
\SearchWP\Queryです(該当する場合) add_hooks- このソースの初期化時にトリガーされます。主に、コンテンツが編集される際の
\SearchWP\Index\Controllerの更新を容易にするためのフックを実装するために使用されます。
フック
\SearchWP\Source の動作をさらに変更するために、いくつかのフックが利用可能です:

