searchwp_live_search_results_entry_data
4.0.0以降
このフィルターフックを使用すると、SearchWP Live Searchの結果テンプレートにレンダリングされる前に、各SearchWP結果エントリのデータをカスタマイズできます。
これは特にLive Searchの出力用であり、開発者がタイトル、パーマリンク、画像マークアップ、抜粋コンテンツなどの結果プロパティを変更できるようにします。
パラメータ
| タイプ | パラメータ | デフォルト | 提供開始 | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 配列 |
$data
|
4.0.0 |
||||||||||||||||||||||
| オブジェクト | $result |
SearchWPの結果エントリオブジェクト | 4.0.0 |
|||||||||||||||||||||
例
All hooks should be added to your custom SearchWP Customizations Plugin.
Live Searchの結果コンテンツに商品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 ); |
メディア添付ファイルの検索結果を親投稿にリンクするように更新する
デフォルトでは、メディア添付ファイルは添付ファイルページにリンクする場合があります。この例では、メディア投稿のLive Search結果が代わりに親投稿の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 ); | |

