Search In Custom Posts

Par hatfab, il y a 8 ans


Bonjour,
J'ai créé un Custom Post Type pour lister des livres. Je voudrais maintenant pouvoir installer un moteur de recherche pour pouvoir fouiller dans tout ce bazar. Une idée ?

Merci à vous

6 réponses

Sparkosis, il y a 8 ans

Salut,

1 . Ajoute une fonction
ici tu peux spécifié archive-search.php

function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'livres' ) //ton post type je dirais livre
{
return locate_template('archive-search.php'); // redirige sur archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
2 . resultats ( search-archive.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(); ?>

Formulaire de recherche

<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="livres" /> <!-- // post type en input hidden --> <input type="submit" alt="Search" value="Search" /> </form> </div>

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

hatfab, il y a 8 ans

Salut Sparkosis, merci pour cette réponse. Mon problème est que mon custom post type est truffé de Advanced Custom Fields et que le moteur de recherche l'ignore totalement. Comment faire pour qu'il prenne en compte ces champs ?

Sparkosis, il y a 8 ans

tu veux afficher ces champs là ?

hatfab, il y a 8 ans

Et surtout les chercher. Pour linstant les requètes sur mon CPT se cantonnent aux champs de base de WP (title, content…) pas aux ACF

hatfab, il y a 8 ans

Merci de ton aide, je vais voir dans cette direction. Merci encore et bonne journée.