WordPressでプラグインにて作成したテーブルも検索条件に含めたいと思っていたのだが、方法がわかったので記載する。最初は、検索したというフックがあるのかな〜と思っていろいろ探していたのだが、そんなものは見当たらず。
そしたらありましたよ!本家に書いてありました。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // 仮想プラグインhogeです array_filter ( 'posts_join' , 'hoge_search_join' ); array_filter ( 'posts_where' , 'hoge_search_where' ); function hoge_search_join( $join ){ global $wpdb , $hoge_db ; // $hoge_dbはプラグイン用dbクラス if (is_search()){ // 検索のときのみテーブルを連結する $join .= " LEFT JOIN {$hoge_db->table} ON " . $wpdb ->posts . ".ID = " . $hoge_db ->table . ".post_id " ; } return $join ; } function hoge_search_where( $where ){ global $wpdb , $hoge_db ; if (is_search()){ // 検索のときのみ検索条件を追加する $where = preg_replace( "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/" , "({$wpdb->posts}.post_title LIKE \\1) OR " . "({$hoge_db->table}.title LIKE \\1) OR " . "({$hoge_db->table}.description LIKE \\1) OR " . "({$hoge_db->table}.summary LIKE \\1) OR " . "({$hoge_db->table}.note LIKE \\1)" , $where ); } return $where ; } |
検索フックではなく(というかそんなの存在しない!)、is_search()メソッドで、検索かどうかをチェックするんか〜。
勉強になった!!