
前提・実現したいこと
wordpressにて、search.phpで検索結果一覧を表示させています。
その検索結果URLを変更したいです。
変更前:
https://domain/?s=keyword
https://domain/カスタム投稿名/search/?s=keyword
条件として、「検索結果には特定のカスタム投稿しか表示させない」があります。
wp_safe_redirect
でリダイレクトさせ、add_rewrite_rule
でURLリライトを行なっているのですが、無限リダイレクトされてしまいます。
該当のソースコード
/** * 検索結果を「特定のカスタム投稿」のみに絞る */ function my_pre_get_posts($query) { if (!is_admin() && $query->is_main_query() && $query->is_search()) { $query->set('post_type', 'カスタム投稿名'); } } add_action('pre_get_posts','my_pre_get_posts'); function my_posts_search($search) { if(is_search()) { $search .= " AND (post_type = 'カスタム投稿名')"; } return $search; } add_filter('posts_search', 'my_posts_search'); /** * リダイレクト */ function nxw_change_search_result_url() { if (is_search() && ! empty($_GET['s'])) { wp_safe_redirect(home_url('/カスタム投稿名/search/?s=') . urlencode(get_query_var('s'))); exit(); } } add_action('template_redirect', 'nxw_change_search_result_url'); /** * URLリライト */ function add_rewrite_url_search() { add_rewrite_rule( '^カスタム投稿名/search/?s=(.*)$', 'index.php?s=$matches[1]', 'top' ); } add_action('init', 'add_rewrite_url_search');

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。