Utilizzo della funzione di ricerca standard con custom post type

12 dic 2016, 13:34:37
Visualizzazioni: 20.6K
Voti: 4

Sto utilizzando il seguente codice HTML per generare una funzione di ricerca su un sito WordPress:

<form method="get" action="<?php bloginfo('url'); ?>">
<fieldset>
<input type="text" name="s" value="" placeholder="cerca&hellip;" maxlength="50" required="required" />
<button type="submit">Cerca</button>
</fieldset>
</form>

Funziona correttamente, ma vorrei che restituisse risultati solo per uno specifico custom post type. Sto utilizzando il template search.php del tema twentysixteen che contiene questo:

<?php  global $wp_query; ?>
<h1 class="search-title"><?php echo $wp_query->found_posts; ?> Risultati trovati per: <span><?php the_search_query(); ?></span></h1>

         <?php if ( have_posts() ) { ?>
           <ul class="results">
             <?php while ( have_posts() ) { the_post(); ?>
                <li>
                  <?php if ( has_post_thumbnail() ) { ?><div class="post-image"><a href="<?php echo get_permalink(); ?>"><?php the_post_thumbnail('thumbnail');?></a></div><?php }?>
                                    <div class="post-content">
                                    <h3><a href="<?php echo get_permalink(); ?>"><?php the_title();  ?></a></h3>
                  <p><?php echo substr(get_the_excerpt(), 0,140); ?>... <a href="<?php the_permalink(); ?>">Leggi altro</a></p>
                                </div>
                </li>
             <?php } ?>
             </ul>
         <?php } ?>

C'è una variabile che posso aggiungere da qualche parte per restituire risultati solo per uno specifico post type? Grazie

0
Tutte le risposte alla domanda 2
2

Ok, ho fatto qualche ricerca in più e si è rivelato abbastanza semplice. Ho solo bisogno di aggiungere un input nascosto nel modulo di ricerca. Lo pubblico qui per chiunque altro stia cercando la risposta:

<form class="search" action="<?php echo home_url( '/' ); ?>">
        <input type="search" name="s" placeholder="Cerca&hellip;">
        <input type="submit" value="Cerca">
        <input type="hidden" name="post_type" value="custom-post-type">
</form>

Ovviamente dovrai sostituire il valore "custom-post-type" con il tuo custom post type.

12 dic 2016 13:44:00
Commenti

Grande bro! Mi hai salvato la giornata!

MD. Atiqur Rahman MD. Atiqur Rahman
14 set 2017 14:54:33

Anche a me ha funzionato, inoltre se vuoi includere diversi tipi di post puoi usare diversi input con nome "post_type[]", ad esempio se vuoi cercare sia nei tipi "product" che "post": <input type="hidden" name="post_type[]" value="product" /><input type="hidden" name="post_type[]" value="post" />

rAthus rAthus
30 set 2020 16:38:11
0

Una soluzione più elegante sarebbe modificare la query principale stessa utilizzando l'azione pre_get_posts.

<?php 

function my_pre_get_posts($query) {

    if( is_admin() ) 
        return;

    if( is_search() && $query->is_main_query() ) {
        $query->set('post_type', 'custom-post-type-name');
    } 

}

add_action( 'pre_get_posts', 'my_pre_get_posts' );

Questa soluzione non richiede alcuna modifica al template.

12 dic 2016 16:53:09