Création d'un formulaire de recherche pour les champs personnalisés
-
-
Jepense que vouspouvez utiliser ceplug-inpourmieux vous aider https://wordpress.org/plugins/wp-custom-fields-search/I think you can use this , plug-in to help you better https://wordpress.org/plugins/wp-custom-fields-search/
- 0
- 2014-10-15
- DINESH BHIMANI
-
Merci ... Mais celan'expliquepas comment créer leformulaire de recherche.Je veux dire queje peuxprogrammer leformulaireen HTML,mais commentpuis-je lefairefonctionner?!Thanks.. But that doesn't explain how to actually create the search form. I mean I can program the form in HTML, but how do i actually get it to function?!
- 0
- 2014-10-15
- absdigital
-
1 réponses
- votes
-
- 2015-02-02
Bien que la réponse de @ MayeenulIslampuissefonctionner,je pense que labonnefaçon defaire une recherche avancéeest d'utiliser le crochet d'action
pre_get_posts
.Étape 1:formulaire de recherche
Cette étape égale à l'étape 1 dans l'autre réponse,vient de changer le
id
du champname
(<input type="text" ...>
utilisépour la recherche sur "s" ,il sera donc utilisé directement comme champ de recherche. Enregistrez ce code dansadvanced-searchform.php
sous votre dossier dethème. Ensuite,utilisezget_template_part( 'advanced', 'searchform' );
pour le charger là où vous voulez qu'il apparaisse dans votrethème:<?php /**`advanced-searchform.php`*/ ?> <form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <h3><?php _e( 'Advanced Search', 'textdomain' ); ?></h3> <!-- PASSING THIS TO TRIGGER THE ADVANCED SEARCH RESULT PAGE FROM functions.php --> <input type="hidden" name="search" value="advanced"> <label for="s" class=""><?php _e( 'Name: ', 'textdomain' ); ?></label><br> <input type="text" value="" placeholder="<?php _e( 'Type the Car Name', 'textdomain' ); ?>" name="s" id="name" /> <label for="model" class=""><?php _e( 'Select a Model: ', 'textdomain' ); ?></label><br> <select name="model" id="model"> <option value=""><?php _e( 'Select one...', 'textdomain' ); ?></option> <option value="model1"><?php _e( 'Model 1', 'textdomain' ); ?></option> <option value="model2"><?php _e( 'Model 2', 'textdomain' ); ?></option> </select> <input type="submit" id="searchsubmit" value="Search" /> </form>
Étape 2: ajoutez desfiltres à la requête de recherche
add_action( 'pre_get_posts', 'advanced_search_query' ); function advanced_search_query( $query ) { if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && ! is_admin() && $query->is_search && $query->is_main_query() ) { $query->set( 'post_type', 'vehicle' ); $_model = $_GET['model'] != '' ? $_GET['model'] : ''; $meta_query = array( array( 'key' => 'car_model', // assumed your meta_key is 'car_model' 'value' => $_model, 'compare' => 'LIKE', // finds models that matches 'model' from the select field ) ) ); $query->set( 'meta_query', $meta_query ); } }
Étape 3: création demodèles (facultatif)
Avec cetteméthode,lemodèle de recherchepar défaut de WordPress sera utilisépourfiltrer les résultats sans avoirbesoin d'une requête secondaire. Si vous souhaitez utiliser un autremodèlepour la recherche avancée,vouspouvez utiliser lefiltre
template_include
. Parexemple,si vous souhaitez utiliser lefichieradvanced-search-template.php
commemodèlepour les résultats duformulaire de recherche avancée:add_action('template_include', 'advanced_search_template'); function advanced_search_template( $template ) { if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && is_search() ) { $t = locate_template('advanced-search-template.php'); if ( ! empty($t) ) { $template = $t; } } return $template; }
Although @MayeenulIslam's answer could work, I think the correct way to do an advanced search is using the
pre_get_posts
action hook.Step 1: Search Form
This step equal to step 1 in the other answer, just changed the
id
of thename
field (<input type="text" ...>
used for search to "s", so it will be used directly for as search field. Save this code inadvanced-searchform.php
under your theme folder. Then, useget_template_part( 'advanced', 'searchform' );
to load it where you want it to appear in your theme:<?php /**`advanced-searchform.php`*/ ?> <form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <h3><?php _e( 'Advanced Search', 'textdomain' ); ?></h3> <!-- PASSING THIS TO TRIGGER THE ADVANCED SEARCH RESULT PAGE FROM functions.php --> <input type="hidden" name="search" value="advanced"> <label for="s" class=""><?php _e( 'Name: ', 'textdomain' ); ?></label><br> <input type="text" value="" placeholder="<?php _e( 'Type the Car Name', 'textdomain' ); ?>" name="s" id="name" /> <label for="model" class=""><?php _e( 'Select a Model: ', 'textdomain' ); ?></label><br> <select name="model" id="model"> <option value=""><?php _e( 'Select one...', 'textdomain' ); ?></option> <option value="model1"><?php _e( 'Model 1', 'textdomain' ); ?></option> <option value="model2"><?php _e( 'Model 2', 'textdomain' ); ?></option> </select> <input type="submit" id="searchsubmit" value="Search" /> </form>
Step 2: Add filters to search query
add_action( 'pre_get_posts', 'advanced_search_query' ); function advanced_search_query( $query ) { if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && ! is_admin() && $query->is_search && $query->is_main_query() ) { $query->set( 'post_type', 'vehicle' ); $_model = $_GET['model'] != '' ? $_GET['model'] : ''; $meta_query = array( array( 'key' => 'car_model', // assumed your meta_key is 'car_model' 'value' => $_model, 'compare' => 'LIKE', // finds models that matches 'model' from the select field ) ) ); $query->set( 'meta_query', $meta_query ); } }
Step 3: Templating (optional)
With this method, the default search template of WordPress will be used filtering the results without the need of a secondary query. If you want to use a different template for advanced search, you can use the
template_include
filter. For example, if you want to useadvanced-search-template.php
file as template for results from the advanced search form:add_action('template_include', 'advanced_search_template'); function advanced_search_template( $template ) { if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && is_search() ) { $t = locate_template('advanced-search-template.php'); if ( ! empty($t) ) { $template = $t; } } return $template; }
-
Mercibeaucouppour lamanière de WordPress.Je viens detravailler avec celui-ciet c'estgénial.:)Thank you very much for the WordPress' way. Just worked with this one and it's awesome. :)
- 3
- 2015-04-30
- Mayeenul Islam
J'ai créé unthèmepour un concessionnaire automobile.Chaque voitureest untype demessagepersonnalisé ("véhicule")et comporteenviron 12 champspersonnalisés avec des élémentstels que lamarque,lemodèle,le kilométrage,letype de carburant,etc.
Donc,fondamentalement,sur lapage d'accueil,je veux unformulaire de recherche contenant des listes déroulantespour Make & amp;Modèleet contienttoutes lesmarques oumodèles disponibles.
Existe-t-il unplugin quipeutfaire cela??
Mercipour votre aide .. celame rendfou depuis des heures !!!!