searchwp_live_search_results_entry_data
4.0.0以降
このフィルターフックにより、SearchWPライブ検索結果テンプレートでレンダリングされる前に、各SearchWP結果エントリのデータをカスタマイズできます。
これはライブ検索の出力専用であり、開発者はタイトル、パーマリンク、画像マークアップ、抜粋コンテンツなどの結果プロパティを変更できます。
パラメータ
| タイプ | パラメータ | デフォルト | 提供開始 | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 配列 |
$data
|
4.0.0 |
||||||||||||||||||||||
| オブジェクト | $result |
SearchWPの結果エントリオブジェクト | 4.0.0 |
|||||||||||||||||||||
例
All hooks should be added to your custom SearchWP Customizations Plugin.
ライブ検索結果コンテンツに商品SKUを含める
この例では、エントリが商品の場合に、WooCommerceの商品SKUを結果コンテンツに追加します。
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 | |
| add_filter( 'searchwp_live_search_results_entry_data', function( $data, $result ) { | |
| if ( $result instanceof \WP_Post && $result->post_type === 'product' ) { | |
| $sku = get_post_meta( $result->ID, '_sku', true ); | |
| if ( ! empty( $sku ) ) { | |
| $data['content'] .= '<p><strong>SKU:</strong> ' . esc_html( $sku ) . '</p>'; | |
| } | |
| } | |
| return $data; | |
| }, 20, 2 ); |
メディア添付ファイルの検索結果を親投稿にリンクするように更新する
デフォルトでは、メディア添付ファイルは添付ファイルページにリンクする場合があります。この例では、メディア投稿のライブ検索結果が代わりに親投稿のURLを指すようにパーマリンクを更新します。
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 | |
| add_filter( 'searchwp_live_search_results_entry_data', function( $data, $result ) { | |
| if ( $result instanceof \WP_Post && $result->post_type === 'attachment' ) { | |
| $parent_id = wp_get_post_parent_id( $result->ID ); | |
| if ( $parent_id ) { | |
| $data['permalink'] = get_permalink( $parent_id ); | |
| } | |
| } | |
| return $data; | |
| }, 20, 2 ); | |

