投稿記事だけを検索で出したいのですがうまくいきません。
現在作っているファイルは以下です。
・search.php
・searchform.php
php
1//search.php 2<?php get_header('info'); ?> 3 <main> 4 <article class="articleWrap"> 5 6 <section class="section"> 7 <h1><?php printf( __( 'Search Results for: %s', 'altitude' ), '<span>' . get_search_query() . '</span>' ); ?>の検索結果</h1> 8 9 <div class="sectionWrap"> 10 <?php 11 if ( have_posts() ) { 12 while ( have_posts() ){ 13 the_post(); 14 15 $category = get_the_category(); 16 $cat_name = $category[0]->cat_name; 17 ?> 18 <div class="column cf"> 19 <span class="<?php $cat=get_the_category(); $cat=$cat[0]; {echo $cat->slug;}?>"><a href="#"><?php echo $cat_name; ?></a></span> 20 <a href="<?php the_permalink(); ?>"> 21 <div class="img2"><img src="<?php the_post_thumbnail_url(); ?>"></div> 22 <h2 class="title"><?php the_title(); ?></h2> 23 <p> 24 <?php if(mb_strlen($post->post_content,'UTF-8')>50){ 25 $content= str_replace('\n', '', mb_substr(strip_tags($post-> post_content), 0, 50,'UTF-8')); 26 echo $content.'……'; 27 }else{ 28 echo str_replace('\n', '', strip_tags($post->post_content)); 29 } 30 ?> 31 </p> 32 </a> 33 </div> 34 <?php 35 } 36 } 37 wp_reset_postdata(); 38 ?> 39 </div> 40 </section> 41 <?php get_sidebar(); ?> 42 </article> 43 </main> 44 45<?php get_footer(); ?> 46
php
1//searchform.php 2<form id="asideForm" action="<?php echo home_url('/'); ?>" method="get"> 3 <input id="lBox" id="s" name="s" type="text" placeholder="フリーワードを入力"/> 4 <button type="submit" id="sbtn"><i class="fas fa-search"></i></button> 5</form>
検索フォームはサイドバーにあります。
検索すると
php
1<h1><?php printf( __( 'Search Results for: %s', 'altitude' ), '<span>' . get_search_query() . '</span>' ); ?>の検索結果</h1>
のところにはちゃんと検索した文字は入るのですが
記事は全然関係ないものがでてくるのですが
何が問題かわかりません。
よろしくお願いいたします。
=============================追記=============================
上記にsearch.phpに書いてあるコード全部載せました。
以下がfunctions.phpです。
php
1//functions.php 2//検索結果に投稿記事のみを表示する 3function custom_search_result($query) { 4 if(!$query->is_admin && $query->is_main_query() && $query->is_search) { 5 $query->set('post_type', 'post'); 6 } 7 return $query; 8} 9add_filter('pre_get_posts','custom_search_result'); 10
回答2件
あなたの回答
tips
プレビュー