Formulaire de recherche avancée avec des filtres pour les taxonomies personnalisées et les champs personnalisés
-
-
Si quelqu'un a des difficultés àmettreen œuvre la solution de Brady ci-dessus (commeje l'aifait),voici unindice:il semble que Wordpress a desproblèmes avec latransmission des données de session,vous devrez doncprobablementfaire quelque chose depluspour lefairefonctionner correctement.Lesproblèmes sont abordésici: http://www.frank-verhoeven.com/using-session-in-wordpress/ Pourmoi,l'installation duplugin "Simple Session Support" de Peter Wooster afait l'affaire.Il y a un lien vers leplugin dans la section commentaires de l'article.If anyone is having difficulty implementing Brady's solution above (as I did) here's a hint: It appears that Wordpress has some problems with passing session data so you will probably have to do something extra to make it work properly. The issues are discussed here: http://www.frank-verhoeven.com/using-session-in-wordpress/ For me installing Peter Wooster's "Simple Session Support" plugin did the trick. There's a link to the plugin in the comments section of the post.
- 0
- 2012-09-14
- SteveR
-
4 réponses
- votes
-
- 2012-02-10
Jepense que vousferiezmieux d'écrire vous-même quelque chose comme ça.
Jetez un œil à: http://www.catalysthomes.co.uk/homes-for-sale/
Lespropriétés sont chargées dans un CPTet j'aimapropre recherchepersonnalisée dans labarre latérale. De cette recherche,il recherche un certainnombre de chosestelles que lestaxonomies,les champspersonnaliséset le classementparprix de date,etc.
Alors,commentpuis-je yparvenir? Je soumets leformulaire à unmodèle depageet àpartir de là,je traite les données depublicationet crée unenouvelle WP_queryen fonction des critères de recherche. J'utilise des sessionspour stocker les variables de recherche afin depouvoirpaginer les résultats.
WP_Queryesttrèspuissant. Jetez un œil: http://codex.wordpress.org/Class_Reference/WP_Query
Ici,vouspouvez utiliser
meta_query
pourinterrogerplusieurs champspersonnaliséset utilisertax_query
pourinterroger vostaxonomies,et il y aplus. Voici comment lemien est conçupour vous donner uneidée.Fichiermodèle:
<?php $temp = $wp_query; $wp_query = NULL; $args = array(); ?> <?php include("functions/Homes-for-sale/propertyrawresults.php"); ?> <?php include("functions/Homes-for-sale/propertysearchresults.php"); ?> <?php $args['post_type'] = "homes-for-sale"; $args['showposts'] = 10; $args['paged'] = $paged; $wp_query = new WP_Query($args); ?> <?php include("functions/Homes-for-sale/propertylistlayout.php"); ?>
Résultatsbruts
<?php if($_POST['sortby']) { $_SESSION['prop_selectedsortby'] = $_POST['sortby']; } switch($_SESSION['prop_selectedsortby']) { case "name-asc": $args['order'] = "ASC"; $args['orderby'] = "title"; break; case "name-desc": $args['orderby'] = "title"; break; case "price-asc": $args['order'] = "ASC"; $args['orderby'] = "meta_value_num"; $args['meta_key'] = "chb_homes_for_sale_specifics_fmv"; break; case "price-desc": $args['orderby'] = "meta_value_num"; $args['meta_key'] = "chb_homes_for_sale_specifics_fmv"; break; case "date-asc": $args['order'] = "ASC"; break; default: /* No need to set arguments here as wp query defaults */ break; } $selectedsortby[$_SESSION['prop_selectedsortby']] = " selected=\"selected\""; ?>
Résultats de la recherche
<?php if( ! empty( $_SESSION['s_property_ptype'] ) ) { $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_types_nbrs', 'value' => $_SESSION['s_property_ptype'] ); } if( ! empty( $_SESSION['s_property_development'] ) ) { $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_ofdevelopment', 'value' => $_SESSION['s_property_development'] ); } if( isset( $_SESSION['s_property_area'] ) && 0 != $_SESSION['s_property_area'] ) { $args['tax_query'][] = array( 'taxonomy' => 'areas', 'field' => 'id', 'terms' => array( (int) $_SESSION['s_property_area'] ), ); } $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bedrooms', 'value' => $_SESSION['s_property_bedrooms_min'], 'compare' => '>=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bedrooms', 'value' => $_SESSION['s_property_bedrooms_max'], 'compare' => '<=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bathrooms', 'value' => $_SESSION['s_property_bathrooms_min'], 'compare' => '>=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bathrooms', 'value' => $_SESSION['s_property_bathrooms_max'], 'compare' => '<=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_fmv', 'value' => $_SESSION['s_property_min_price'], 'compare' => '>=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_fmv', 'value' => $_SESSION['s_property_max_price'], 'compare' => '<=', 'type' => 'SIGNED' ); ?>
Miseen page de la liste Juste uneboucle WP standardpour afficher desextraitset desinformations depublication.
I think something like this you would be best writing yourself.
Take a look at: http://www.catalysthomes.co.uk/homes-for-sale/
Properties are loaded into a CPT and I have my own custom search in the sidebar. Of that search its searching a number of things such as taxonomies, custom fields and ordering by date price etc.
So how do I achieve this? I submit the form to a page template and from there I deal with the post data and build a new WP_query based on the search criteria. I use sessions to store the search variables so that I can paginate the results.
WP_Query is very powerful. Take a look: http://codex.wordpress.org/Class_Reference/WP_Query
In there you can use
meta_query
to query multiple custom fields and usetax_query
to query your taxonomies, plus there is more. Below is how mine is built to give you an idea.Template File:
<?php $temp = $wp_query; $wp_query = NULL; $args = array(); ?> <?php include("functions/Homes-for-sale/propertyrawresults.php"); ?> <?php include("functions/Homes-for-sale/propertysearchresults.php"); ?> <?php $args['post_type'] = "homes-for-sale"; $args['showposts'] = 10; $args['paged'] = $paged; $wp_query = new WP_Query($args); ?> <?php include("functions/Homes-for-sale/propertylistlayout.php"); ?>
Raw Results
<?php if($_POST['sortby']) { $_SESSION['prop_selectedsortby'] = $_POST['sortby']; } switch($_SESSION['prop_selectedsortby']) { case "name-asc": $args['order'] = "ASC"; $args['orderby'] = "title"; break; case "name-desc": $args['orderby'] = "title"; break; case "price-asc": $args['order'] = "ASC"; $args['orderby'] = "meta_value_num"; $args['meta_key'] = "chb_homes_for_sale_specifics_fmv"; break; case "price-desc": $args['orderby'] = "meta_value_num"; $args['meta_key'] = "chb_homes_for_sale_specifics_fmv"; break; case "date-asc": $args['order'] = "ASC"; break; default: /* No need to set arguments here as wp query defaults */ break; } $selectedsortby[$_SESSION['prop_selectedsortby']] = " selected=\"selected\""; ?>
Search Results
<?php if( ! empty( $_SESSION['s_property_ptype'] ) ) { $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_types_nbrs', 'value' => $_SESSION['s_property_ptype'] ); } if( ! empty( $_SESSION['s_property_development'] ) ) { $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_ofdevelopment', 'value' => $_SESSION['s_property_development'] ); } if( isset( $_SESSION['s_property_area'] ) && 0 != $_SESSION['s_property_area'] ) { $args['tax_query'][] = array( 'taxonomy' => 'areas', 'field' => 'id', 'terms' => array( (int) $_SESSION['s_property_area'] ), ); } $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bedrooms', 'value' => $_SESSION['s_property_bedrooms_min'], 'compare' => '>=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bedrooms', 'value' => $_SESSION['s_property_bedrooms_max'], 'compare' => '<=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bathrooms', 'value' => $_SESSION['s_property_bathrooms_min'], 'compare' => '>=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_bathrooms', 'value' => $_SESSION['s_property_bathrooms_max'], 'compare' => '<=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_fmv', 'value' => $_SESSION['s_property_min_price'], 'compare' => '>=', 'type' => 'SIGNED' ); $args['meta_query'][] = array( 'key' => 'chb_homes_for_sale_specifics_fmv', 'value' => $_SESSION['s_property_max_price'], 'compare' => '<=', 'type' => 'SIGNED' ); ?>
List Layout Just a standard WP loop to show post excerpts and info.
-
Salut Brady,Thxpour cetexemple.Puis-je vous demander,sipossible,departager leformulaire?Dans ce document,à quelle URL le soumettez-vous?Hi Brady, Thx for this example. Could I ask you, if possible, to share the form? In it, what URL are your submitting it to?
- 0
- 2012-06-28
- salocin
-
@salocin - Cetteinformationpeut être obtenueen regardant la source de lapage sur l'URL donnéeen réponse@salocin - That information can be gotten by looking at the source of the page on the given URL in answer
- 0
- 2012-06-28
- Scott
-
thx Brady,alors l'URL dumodèle depage?thx Brady, so url of the page template?
- 0
- 2012-06-28
- salocin
-
Leformulaireest sur catalyseurhomes.co.uk.Vouspouvez afficher la sourceet voir comment leformulairefonctionneThe form is on catalysthomes.co.uk. You can view source and see how the form works
- 0
- 2012-06-28
- Scott
-
Vérifiez-vous si lestaxonomiespersonnalisées contiennent despublicationsen tant que champpersonnalisé?Comment remplissez-vousexactement leformulaire de recherche?@BradymerciAre you check to see if the custom taxonomies have posts with them as a custom field? How exactly are you populating the search form? @Brady thanks
- 0
- 2016-02-19
- Phil Hudson
-
- 2012-01-11
Essayez leplugin Taxonomy Picker ensemble avec Relevanssi.Combinaison detueur.
http://www.squidoo.com/taxonomy-picker-wordpress-plugin http://wordpress.org/extend/plugins/relevanssi/
Try Taxonomy Picker plugin together with Relevanssi. Killer combination.
http://www.squidoo.com/taxonomy-picker-wordpress-plugin http://wordpress.org/extend/plugins/relevanssi/
-
celane prendpasen compte les relations deposte,qui sont lapartie difficile demonproblème.Ilestbeaucoupplusfacile defiltrer les articlespartaxonomie,je dois lesfiltrerpour acheter lataxonomie (ou champpersonnalisé) d'un article associé.this doesn't take into account post relationships, that being the difficult part of my problem. It's way easier to filter posts by taxonomy, I need to filter them buy the taxonomy (or custom field) of a related post.
- 0
- 2012-01-14
- pax
-
- 2012-01-10
Jetez un œil auplugin Relevanssi,ilpourraitfaire ce que vous recherchez: http://wordpress.org/extend/plugins/relevanssi/
Take a look at Relevanssi plugin, it might do what you are looking for: http://wordpress.org/extend/plugins/relevanssi/
-
Celane convientpas à l'approche dontj'aibesoin (avec desfiltres spécifiques),mais c'est unplugintrèsintéressant,cela ressemble à unexcellent remplacementpour lafonction de recherchepar défaut,mercibeaucoup de l'avoir signalé.It doesn't suit the approach that I need (with specific filters), but it's a very interesting plugin, it looks like a great replacement for the default search function, thanks very much for pointing it out.
- 0
- 2012-01-10
- pax
-
- 2012-09-14
Si quelqu'un a des difficultés àmettreen œuvre la solution de Brady ci-dessus (commeje l'aifait),voici unindice:il semble que WordPress a desproblèmes avec latransmission des données de session,vous devrez doncprobablementfaire quelque chose depluspour lefairefonctionner correctement.Lesproblèmes sont discutésici
Dansfunctions.php:
function init_sessions() { if (!session_id()) { session_start(); } } add_action('init', 'init_sessions');
Dans votremodèle:
/** * Enable sessions */ if (!session_id()) session_start();
Pourmoi,l'installation duplugin " Simple Session Support " de Peter Wooster apermistour.
If anyone is having difficulty implementing Brady's solution above (as I did) here's a hint: It appears that WordPress has some problems with passing session data so you will probably have to do something extra to make it work properly. The issues are discussed here
In functions.php:
function init_sessions() { if (!session_id()) { session_start(); } } add_action('init', 'init_sessions');
In your template:
/** * Enable sessions */ if (!session_id()) session_start();
For me installing Peter Wooster's "Simple Session Support" plugin did the trick.
-
Salut Steve.Merci d'avoirpublié votrepremière réponseici.Heureux de vous avoir à WPSE.Pour référencefuture,les réponsesne doiventpaspivoter complètement sur un lienexterne.Si le lienest désactivé,votre réponse devientbénigne.Pourriez-vousmettre àjour votre réponse avec quelquesexemples d'extraits de codepertinents?Hi Steve. Thanks for posting your first answer here. Glad to have you at WPSE. For future reference, answers shouldn't pivot completely on an external link. If the link is disabled, your answer becomes benign. Would you mind updating your answer with a couple of sample relevant code snippets?
- 1
- 2012-09-16
- Brian Fegter
Je voudrais créer unformulaire de recherche avancéepour untype depublicationpersonnalisé spécifique,avec desfiltrespour lestypes depublicationpersonnalisés,les champspersonnalisés,lestaxonomiespersonnaliséeset pour lespropriétés destypes depublicationpersonnalisées distinctes (champset taxonomies) qui seront des liens aupremiertype depublication à l'aide d'un champ de relationpersonnalisé.
J'ai récemment commencé avec lestypes depublications,les champset lestaxonomiespersonnalisés de WP,je l'aimejusqu'àprésent,maispouren tirer lemeilleurparti,j'aimeraispouvoir le rechercher correctement. Dois-je lefairemanuellement? Si oui,comment?
PS. Si cela compte,j'utilise lesplugins: Champspersonnalisés avancés et Interface utilisateur dutype demessagepersonnalisé .
Ci-dessous,j'aifait unemaquette d'unexemple de l'apparence dufiltrageet de son lien avec lestypes depublications ci-dessus.