Help us understand the problem. What is going on with this article?

【wordpress】サイト内検索でカスタム投稿を含める

More than 3 years have passed since last update.

ロジック忘れないように忘備録

functions.phpなどに

/**
 * 検索ロジックカスタマイズ
 *
 */
function customSearcher($search, $wp_query) {
    if ( $wp_query->is_main_query() && $wp_query->is_search() && !is_admin() ) {
        $q = $wp_query->query_vars;
        $n = null;
        if (!empty($q['s'])) {

            $keyword = mb_convert_kana($q['s'], 's');
            $keywords = preg_split('/[\s]+/', $keyword, -1, PREG_SPLIT_NO_EMPTY);

            foreach ($keywords as $word) {
                $n = " OR ( pm.meta_key IN ( 'search_data' ) AND pm.meta_value LIKE '%" . $word . "%') ";

                $search = str_replace( "post_content LIKE '%". $word .  "%')", "post_content LIKE '%". $word .  "%')" . $n , $search );
            }

        }

    }
    return $search;
}
add_filter('posts_search', 'customSearcher', 10, 2);
_shogo_
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした