サポート » マルチサイト » 子サイトを含めた検索フォーム

  • inoue0213

    (@inoue0213)


    子サイトを含めた検索フォームの実装を調べていたのですが、思うようにいきません。
    どの様な記述をすればよろしいでしょうか?
    すでに質問等されているトピックスがありましたら申し訳ございません。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • inoue0213さん

    以下のソースを「search.php」でカスタマイズしています。
    いかがでしょうか。

    
    <div id="main_col">
    <?php
    // 検索クエリを取得
    $search_result = get_search_query();
    // マルチサイト内を検索
    $search_results = search_multisite($search_result);
    
    function search_multisite($query_string) {
    add_action( 'pre_get_posts', 'my_pre_get_posts' );
    
    function my_pre_get_posts( $query ) {
    $query->set( 'posts_per_page' , 3 );//1ページ当たりの数
    }
    
       // 文字列エンコード
       $query_string = esc_attr($query_string);
       $search_result = array();
    
       // ネットワーク上のブログを取得
       $blogs = get_blogs_of_user( 1, true);
       foreach ( $blogs as $blog ):
         // ブログを切り替える
         switch_to_blog($blog->userblog_id);
         // 切り替えたブログでqueryを実行
         $search = new WP_Query(
    	   array(
    	     's' => $query_string,
    		 'post_type' => array('post','page',)
    		  )
    	 );
    
         if ($search->found_posts > 0):
           foreach ( $search->posts as $post ):
             // 投稿記事に関連するグローバル変数を設定する
             setup_postdata($post);
             array_push($search_result, $post);
    
           endforeach;
         endif; 
    
       endforeach;
    
       return $search_result;
       restore_current_blog();
       
    
    // END function search_multisite($query_string)
     }
    
    ?>      
    
    <h2>検索結果</h2>
    <p>“<?php the_search_query(); ?>”に関連する検索の結果 (<?php echo count($search_results); ?> 件)</p>
    
    <?php
    foreach ( $search_results as $post ) :
    //the_post;
    setup_postdata($post);
    ?>
    <!--検索結果のループ-->
    <div class="searchList" style="border:solid 1px red; margin-bottom:15px;">
      <div class="article">
        <h3><a href="<?php echo $post->guid; ?>"><?php the_title(); ?></a></h3>
        <p>
        
        <?php
        $id =get_the_ID();
    	//echo $post->post_content;
    	the_excerpt();
    
    	?>
        </p>
        <p class="detail"><a href="<?php echo $post->guid; ?>">詳細はこちら</a></p>
      </div>
    </div>
    <!--/検索結果のループ-->
    
    <?php
    endforeach;
    wp_reset_query();
    
    ?>
     <div id="next_post"><?php next_posts_link(); ?></div>
    </div><!-- END #main_col -->
    
    
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「子サイトを含めた検索フォーム」には新たに返信することはできません。