Query di Ricerca Personalizzata

8 apr 2014, 18:18:22
Visualizzazioni: 36.3K
Voti: 6

Vorrei impostare una pagina di ricerca personalizzata che faccia quanto segue:

L'utente seleziona diversi elementi in un form che vorrebbe vedere nei risultati della ricerca (essenzialmente scegliendo da un elenco di tag).

Vengono restituiti i risultati che corrispondono a tutti i tag selezionati (usando AND non OR).

Un esempio specifico sarebbe:

Restituire tutti i post nella categoria "Area" dove i tag = "scuola elementare" AND "parco"

  1. Devo dare un nome particolare al mio form di ricerca?
  2. Nella pagina dei risultati di ricerca, come devo codificare la query personalizzata in modo che ottenga tutti i post all'interno della categoria Area e che abbiano tutti i tag che l'utente ha scelto nel form di ricerca?
// Esempio di query personalizzata
$args = array(
    'category_name' => 'area',
    'tag_slug__and' => array('scuola-elementare', 'parco'), // Usa AND per i tag
    'post_type' => 'post',
    'posts_per_page' => -1
);

$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        // Mostra i risultati
        the_title();
        the_content();
    endwhile;
    wp_reset_postdata();
else :
    echo 'Nessun risultato trovato';
endif;
6
Commenti

Molta confusione in quello che ho letto. Ho visto che dovrei usare qualcosa come query_posts('cat=32&tag=hs1+hs1&showposts=5'); e ho visto che dovrei usare qualcosa come `<?php

$the_query = new WP_Query( 'cat=Neighborhood&tag=elementary school+park' );

// The Loop while ( $the_query->have_posts() ) : $the_query->the_post();
the_title(); the_content(); endwhile;

wp_reset_postdata();

?>`

Peanut Peanut
8 apr 2014 18:47:22

e ho visto che dovrei usare qualcosa come $query = array ( 'paged' => 1, 'posts_per_page' => '5', 'offset' => 0, 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'post_type' => array ( 'post' => 'post', ), 'cat' => '35', 'tag__and' => array ( 0 => 36, 1 => 39, ), ); ... fondamentalmente sono solo estremamente confuso.

Peanut Peanut
8 apr 2014 18:47:38

@Peanut Due note: Se hai informazioni aggiuntive, per favore inseriscile come [modifiche] invece che commenti. Non tutti leggono i commenti e questi vengono periodicamente ripuliti. Secondo, per favore usa la formattazione (backtick) per il codice nei commenti. Hai un link "aiuto" proprio accanto al modulo dei commenti qui. Grazie.

kaiser kaiser
8 apr 2014 18:58:15

Usa WP_Query e il suo parametro s, non dimenticare di chiamare wp_reset_postdata dopo aver terminato con questa query :-) REF: https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter

jave.web jave.web
20 gen 2017 14:04:11

Per favore consulta la stessa domanda su stackoverflow https://stackoverflow.com/questions/9523881/how-do-i-modify-search-query-in-wordpress/57786703#57786703

Amit Kumar PRO Amit Kumar PRO
4 set 2019 13:42:32

Per favore controlla la stessa domanda su stackoverflow https://stackoverflow.com/questions/9523881/how-do-i-modify-search-query-in-wordpress/57786703#57786703

Amit Kumar PRO Amit Kumar PRO
4 set 2019 13:43:56
Mostra i restanti 1 commenti
Tutte le risposte alla domanda 2
0
11

1) Puoi utilizzare i template search.php e searchform.php come punto di partenza. Creare una Pagina di Ricerca Codex

2) Per quanto riguarda la query personalizzata, puoi usare l'hook pre_get_posts per verificare se ti trovi in una pagina di ricerca, poi ottieni i tuoi valori da $_GET e modifica la query di conseguenza. Riferimento Azione - pre_get_posts

Ci sono tantissimi tutorial online e domande su questo exchange che possono aiutarti. Alcuni sono Semplici e altri più Complessi. Dovrai fare delle ricerche approfondite per realizzare questo obiettivo. Spero sia d'aiuto!

8 apr 2014 18:24:48
0

Per creare una ricerca personalizzata, dovrai includere questi input nel tuo HTML. Puoi utilizzare gli attributi name e value per passarli all'URL.

<input type="hidden" class="category" name="category_name" value="recording">
<input type="hidden" class="type" name="post_type" value="post">
<input type="text" class="search-bar" id="global-search"><button class="search-submit" type="submit" id="search-submit">

Poi nel tuo file di script dovrai costruire l'URL.

// aggiorna il tipo di ricerca
let type = $('input.type').val();
if(type === 0 || type == undefined){
  type = "";
}
// aggiorna la categoria di ricerca
let category = $('input.category').val();
if(category === 0 || category == undefined){
  category = "";
}
// aggiorna la parola chiave di ricerca
keyword = '/?s=' + $(this).val() + '&post_type=' + type + '&category_name=' + category;
$('.search-keyword').attr('data-target','/cimuk' + keyword);
$('.search-keyword .label').text('Cerca ' + '"' + $(this).val() + '"');

//chiama questa funzione al submit o all'invio
function goKeyword(){
    window.location.href = $('.search-keyword').data('target')
}

Poi puoi aggiungere questo filtro al tuo functions.php. Questo prenderà le variabili dall'URL e le passerà alla query di ricerca.

//Filtro di ricerca per tipo di post
function mySearchFilter($query) {
    $post_type = $_GET['post_type'];
    $category = $_GET['category_name'];
    if (!$post_type) {
        $post_type = 'any';
    }
    if (!$category){
        $category = null;
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
        $query->set('category_name', $category);
    };
    return $query;
};


add_filter('pre_get_posts','mySearchFilter');

I risultati dovrebbero caricarsi sul tuo search.php

14 nov 2019 23:25:36