J.Tural
J.Tural

Reputation: 925

Wordpress add search to custom post type archive

I have a website with many custom post type for example players teams ...

for each one of them I have archive pages like : player-archive.php

in custom post type archive pages I want to add to the top a search ( and filter if possible ).

PS : I see other Questions with so code about the search but it don't work and there is no answer to the question and others with plugins but they are general to improve all the search in the site .

so first of all is it possible if there is any suggestions for plugin also will be great .

Upvotes: 2

Views: 7260

Answers (2)

PHPExpert
PHPExpert

Reputation: 943

These 3 steps worked for me:

Step 1: Create a form for search

Step 2: In your function.php add the following code:

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

add_filter('pre_get_posts','searchfilter');

Step 3: Create a search.php file ad add:

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

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

           <?php }?>
    <?php } else { ?>
<?php } ?>
<?php endwhile; else: ?>
<?php endif; ?>

For your reference:

http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/

http://wpsnipp.com/index.php/template/create-multiple-search-templates-for-custom-post-types/

Hope this helps!

Upvotes: 1

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17944

You have 3 easy steps to go

let's say your custom post type is "players"

1 . Add Function Codes
here you can specify the archive-search.php

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

2 . create search result template for custom post type ( search-archive.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>
        </div>
        <?php endwhile; ?>
        <?php endif; ?>
    </div><!-- content -->
</div><!-- contentarea -->   
<?php get_sidebar(); ?>
<?php get_footer(); ?>

3 . Build Search Form
In this Search Form the value "players" is hidden and it will search only player posts.

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

Upvotes: 3

Related Questions