Wordpressのカスタム投稿の絞り込み検索を実装したいと思っています。
検索ページでカスタム投稿のタグを取得するところまでは問題ないのですが、検索結果がうまくいきません。
プログラムに関してはまだ初心者でどこの部分に問題があるかのご指摘をいただけたら嬉しいです。
よろしくお願いいたします。
phpのバージョンは5.4
wordpressのバージョンは4.9.1
です。
カスタム投稿の設定はこのようにしています。
function bourgeon_create_post_type(){ register_post_type( 'class', array( 'labels' => array( 'name' => 'クラス', 'singular_name' => 'クラス', ), 'public' => true, 'menu_position' => 5, 'has_archive' => true, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ), ) ); } function bourgeon_create_taxonomies() { $labels = array ( 'name' => 'クラスの分類', 'singular-name' => 'クラスの分類', ); register_taxonomy( 'type', array( 'class' ), array ( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, ) ); //カスタムタクソノミー、タグタイプ register_taxonomy( 'class-tag', 'class', array( 'hierarchical' => false, 'update_count_callback' => '_update_post_term_count', 'label' => 'クラスのタグ', 'singular_label' => 'クラスのタグ', 'public' => true, 'show_ui' => true, 'has_archive' => true, ) ); } add_action( 'init', 'bourgeon_create_post_type', 1 );
function.phpにこちらのコードを追記しました。
function custom_search($search, $wp_query ) { //query['s']があったら検索ページ表示 if ( isset($wp_query->query['s']) ) $wp_query->is_search = true; return $search; } add_filter('posts_search','custom_search', 10, 2);
検索ページは
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>"> <label for="s" class="assistive-text">検索</label> <input type="text" name="s" id="s" placeholder="検索" /> <div>タグまたはカスタムタクソノミー</div> <?php $taxonomy_name = 'class-tag'; $taxonomys = get_terms($taxonomy_name); if(!is_wp_error($taxonomys) && count($taxonomys)): foreach($taxonomys as $taxonomy): $tax_posts = get_posts(array('post_type' => get_post_type('クラス'), 'taxonomy' => $taxonomy_name, 'term' => $taxonomy->slug ) ); if($tax_posts): ?> <label><input type="checkbox" name="post_tag[]" value="<?php echo $taxonomy->slug; ?>"><?php echo $taxonomy->name; ?></label><br> <?php endif; endforeach; endif; ?> <input type="submit" value="検索" />
seartch.phpは
<?php $s = $_GET['s']; $post_tag = $_GET['class-tag']; //tax_query用 if($post_tag){ $taxquerysp[] = array( 'taxonomy'=>'class-tag', 'terms'=> $post_tag, 'include_children'=>false, 'field'=>'slug', 'operator'=>'AND' ); } ?> <div>検索条件</div> <?php if($s){ ?>検索キーワード:<?php echo $s; ?><br><?php } ?> <?php if (is_array($post_tag)) { ?>タグ(カスタムタクソノミー):<?php foreach($post_tag as $val){ if ($val === end($post_tag)) { echo get_term_by('slug',$val,"class-tag")->name; }else{ echo get_term_by('slug',$val,"class-tag")->name.", "; } } } ?> <div>検索結果</div> <?php query_posts( array( 'tax_query' => $taxquerysp, 's' => $s, ) ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div><?php the_title(); ?></div> <div><?php the_content(); ?></div> <?php endwhile; else : ?> <div>該当なし</div> <?php endif; wp_reset_query(); ?>
という記述にしております。
検索結果としては
1.指定したいカスタム投稿とは別のもう一つのカスタム投稿
2.固定ページ
3.通常の投稿
のリストができてしまいます。
回答1件
あなたの回答
tips
プレビュー