web-dev-qa-db-ja.com

カスタム投稿タイプのカスタム検索を作成する方法

ブログ投稿用の検索フィールドがありますが、カスタム投稿タイプ用にもう1つ必要です。異なる検索結果のレイアウト _を使用して、このカスタム検索フォームを作成する方法を教えてください。

42
robert

これが私が試した3つのステップからなる解決策です。あなたのカスタム投稿タイプが " products "だとしましょう。

1。 機能コードの追加 ここでarchive-search.phpを指定できます

function template_chooser($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'products' )   
  {
    return locate_template('archive-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser');    

2。作成 検索結果のテンプレート カスタム投稿タイプの場合(archive-search.php)

        <?php
        /* Template Name: Custom Search */        
        get_header(); ?>             
        <div class="contentarea">
            <div id="content" class="content_right">  
                     <h3>Search Result for : <?php echo "$s"; ?> </h3>       
                     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
                <div id="post-<?php the_ID(); ?>" class="posts">        
                     <article>        
                    <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>        
                    <p><?php the_exerpt(); ?></p>        
                    <p align="right"><a href="<?php the_permalink(); ?>">Read     More</a></p>    
                    <span class="post-meta"> Post By <?php the_author(); ?>    
                     | Date : <?php echo date('j F Y'); ?></span>    

                    </article><!-- #post -->    
                </div>
        <?php endwhile; ?>
    <?php endif; ?>




           </div><!-- content -->    
        </div><!-- contentarea -->   
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>
  1. 検索フォームを作成します
    この検索フォームでは、 "products"という値は非表示になっており、 product postsのみが検索されます。

     <div>   
        <h3>Search Products</h3>
        <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
        <input type="text" name="s" placeholder="Search Products"/>
        <input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
        <input type="submit" alt="Search" value="Search" />
      </form>
     </div>
    

もっと、私はあなたをここにリンクしたいと思います
http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/ /

58
Ronald

これは私のために働くものです。それほどきれいではありませんが、私はこれらの他の答えのどれもうまくいくことができなかった。

カスタム投稿タイプの検索フォーム:

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
    <label>
        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
        <input type="hidden" name="post_type" value="book" />
    </label>
    <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>

functions.php:

function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
                if($type == 'book') {
                    $query->set('post_type',array('book'));
                }
        }       
    }
return $query;
}
add_filter('pre_get_posts','searchfilter');

search.phpの場合:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
    <?php if(isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
           if($type == 'book') {?>

               /* Format for "book" custom post type */

           <?php } else { ?>

               /* Format for custom post types that are not "book,"
               or you can use elseif to specify a second post type the
               same way as above. Copy the default format here if you
               only have one custom post type. */

           <?php } ?>
    <?php } else { ?>

              /* Format to display when the post_type parameter
              is not set (i.e. default format) */
<?php } ?>
<?php endwhile; else: ?>

/* What to display if there are no results. */

<?php endif; ?>

当然のことながら、3つの場所すべてで、 "book"をあなたのカスタム投稿タイプに置き換える必要があります。

これが誰かに役立つことを願っています!

6
Shoelaced

より具体化された短いコード

 function template_chooser($template)   
{    
  global $wp_query; 
  $post_type = $wp_query->query_vars["pagename"];   
  if( isset($_GET['s']) && $post_type == 'products' )   
  {

    return locate_template('archive-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser'); 

通常の検索とカスタム投稿タイプの検索では、2つの異なる形式を使用することを検討していました。

私のカスタム投稿タイプは、通常のページとは異なるヘッダーを使用しています。通常のページでは、検索フォームの呼び出しは次のようになります。

<?php get_search_form(true); ?>

そして、私の検索フォームへのカスタム投稿タイプヘッダの呼び出しは、次のとおりです。

<?php get_template_part('search','library'); ?>

追加のフィールドがあります。

<input type="hidden" name="post_type" value="library" /> //Where "library" is my custom post type.

関数ファイルには、あなたが提供した以下のコードがあります。

/** Custom Search for Library */
function search_library($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'library' )   
  {
    return locate_template('search-library.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'search_library');

これは、検索フォームがカスタムフィールド内で検索を実行しているかどうかを検出するため、カスタムテンプレートで検索を表示します。それ以外の場合は通常のテンプレートを使用します。

編集:何に関係なくtrueを返すはずだったget_search_form()関数呼び出しを修正しました。

2
Fliberty

空の入力検索の問題を解決するには、これを関数コードで代用します。

function template_chooser($template)   
{    
 global $wp_query;   
 $post_type = get_query_var('post_type');   
 if( isset($_GET['s']) && $post_type == 'products' )   
 {
  return locate_template('archive-search.php');  //  redirect to archive-search.php
 }   
 return $template;   
}
add_filter('template_include', 'template_chooser');
1
ciccioformaggio