SearchWP Documentation

インストールガイドを表示したり、ナレッジベースを参照したり、SearchWPの多くのフックについて確認したりできます。

\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)
ラベル。singularplural のキーを持つ配列。(デフォルト: [])
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 の動作をさらに変更するために、いくつかのフックが利用可能です:

今日からより良いWordPress検索エクスペリエンスを作成しましょう

役に立たない検索結果で訪問者を失うことはもうありません。SearchWPを使用すると、独自のスマートなWordPress検索をすばやく簡単に作成できます。

Get SearchWP Now
複数の検索エンジンアイコン