Includi il termine della tassonomia personalizzata nella ricerca
-
-
Norcross,puoi aggiungere qualchefeedback alla rispostaproposta da Jan?Probabilmente stai cercando unplugin chefacciail lavoro?Nocross, can you add some feedback to the answer proposed by Jan? Are you probably looking for a plugin that does the job?
- 0
- 2010-11-06
- hakre
-
Hofinitoper abbandonareilpiano.Dalmomento che avevo creato 3funzioni di ricerca separate (inbase aesigenze diversein determinate aree),tuttii plugin che hotestato hanno rotto quelle.Allafine,ho detto al cliente diincluderei termininel contenuto se volevano chefosse ricercabile.I ended up ditching the plan. Since I had created 3 separate search functions (based on different needs in certain areas), all the plugins I tested broke those. In the end, I told the client to include terms in the content if they wanted it searchable.
- 0
- 2010-11-13
- Norcross
-
6 risposta
- voti
-
- 2010-12-15
Consiglierei ancheilplugin Cercatutto ,ma se vuoiimplementarlo utilizzando lafunzione di ricerca di WP,eccoil codice che sto usandonelmiotema Atom:
// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb; if (is_search()) $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')"; return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
Sibasa sulplug-in di ricercatag: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23
I would recommend the Search Everything plugin too, but if you want to implement this using WP's search function, here's the code I'm using in my Atom theme:
// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb; if (is_search()) $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')"; return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
It's based on the Tag-Search plugin: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23
-
Questo èfantastico: comepuòesseremodificato questo codiceperescludere un array di IDtassonomia dalla ricerca?This is great-- how can this code be modified to exclude an array of taxonomy IDs from the search?
- 1
- 2012-01-06
- HandiworkNYC.com
-
Vanotato chei callback delfiltroper questi hook accettano * 2 * argomenti;il secondopertutti è l'istanza _WP_Query_ che vienepassataper riferimento.Qualsiasi controlloper `is_search ()` o altre chiamate almetodo _WP_Query_ (`is_search ()` `is_home ()`ecc.) Dovrebbe sempreessere chiamato direttamente sull'istanza della query (es. `$ Query->is_search ()` assumendo cheilnome della variabile diistanza è "$ query"nellafirma di callback)piuttosto che lafunzionemodello chefarà sempre riferimento alla queryprincipale,non alla queryper cui èin esecuzioneilfiltro.It should be noted that the filter callbacks for these hooks accept *2* arguments; the 2nd for all of them being the _WP_Query_ instance which is passed by reference. Any checks for `is_search()` or other _WP_Query_ method calls (`is_search()` `is_home()` etc.) should always be called directly on the query instance (eg. `$query->is_search()` assuming the name of the instance variable is `$query` in the callback signature) rather than the template function which will always refer to the main query, not the query that the filter is running for.
- 0
- 2014-06-07
- Evan Mattson
-
Inoltre,probabilmentenon è unabuonaideainserire la stringa di ricercanon disponibilepubblicamente direttamentein una query SQL ... [lettura consigliata] (http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks)Also, probably not a good idea to inject the raw publicly available search string directly into an SQL query... [recommended reading](http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks)
- 6
- 2014-06-07
- Evan Mattson
-
Vorrei solo aggiungere che questo ha un conflitto con WPMLperché WPML utilizzagià 'T'nellaparte di unione,quindi l'uso di qualcosa dipersonalizzatoinvece ditr,tt et risolve questoproblemaI would just like to add that this has a conflict with WPML because WPML alredy uses 'T' in join part, so using something custom instead of tr, tt and t fixes this problem
- 0
- 2016-02-09
- Bobz
-
@EvanMattson - hai commentato sopra che "probabilmentenon è unabuonaideainiettare la stringa di ricercanon disponibilepubblicamente direttamentein una query SQL."Comefare anegare quel rischio?Ho lettoil link che haifornitomanon sono riuscito a vedere come si collega alla risposta originale.Moltegrazie Em.@EvanMattson — you commented above that it's "probably not a good idea to inject the raw publicly available search string directly into an SQL query." How would go about negating that risk? I read the link you provided but couldn't see how that links to the original answer. Many thanks Em.
- 1
- 2020-04-05
- The Chewy
-
@TheChewy la risposta sopramostra `get_search_query ()` usata direttamentenell'istruzione MySQL della clausola WHERE.Questo codice è vulnerabile agli attacchi SQLinjection,in cui chiunquepuòeseguire query SQL arbitrarie sultuo sitofacendolepassare attraversoil campo di ricerca.Ovviamente è così chefunzionano le ricerchein una certamisura,mailpuntoimportante è che questoinput deveesserepreparato adeguatamentepergarantire che vengainterpretato cometermini di ricercae non comeistruzioni SQL.Questo è ciò che èilmetodo `$ wpdb->prepare ()`peril quale è descrittonella suddetta lettura raccomandata@TheChewy the answer above shows `get_search_query()` used directly in the MySQL statement of the WHERE clause. This code is vulnerable to SQL injection attacks, where anyone can execute arbitrary SQL queries on your site by passing them through the search field. Obviously that's how searches work to some degree, but the important point is that this input needs to be prepared properly to ensure it is interpreted as search terms and not SQL statements. This is what the `$wpdb->prepare()` method is for which is described in said recommended reading
- 0
- 2020-04-19
- Evan Mattson
-
@TheChewypuoi anchetrovare alcunibuoniesempinella documentazione di `wpdb ::prepare ()` qui: https://developer.wordpress.org/reference/classes/wpdb/prepare/@TheChewy you can also find some good examples in the docs for `wpdb::prepare()` here: https://developer.wordpress.org/reference/classes/wpdb/prepare/
- 0
- 2020-04-19
- Evan Mattson
-
@EvanMattson Ciao Evan,grazieper l'input.Sononuovo siaperphp cheperil codice WPpersonalizzatoe questo è andato così lontanonellamiatesta.Sarestiin grado di duplicareil codice con l'aggiunta di `$ wpdb->prepare ()` senon ètroppo difficile?Penso chepasseranno settimaneprima di avvicinarmi a questogenere di cose.@EvanMattson Hi Evan, thanks for the input. I'm new to both php and custom WP code and that has gone so far over my head. Would you be able to duplicate the code with the `$wpdb->prepare()` added if it isn't too hard? I think it'll be weeks before I get close to understanding that sort of thing.
- 1
- 2020-04-21
- The Chewy
-
- 2010-10-07
È questa la ricerca standard di WordPress?Perché non sembraincluderetassonomie (nemmenostandard,come categoriee tag)nella ricerca.Il codice cercain
post_title
epost_content
,ma se vuoiincludere qualcos'altro devi collegarti alfiltroposts_search
.Is this the standard WordPress search? Because that doesn't seem to include taxonomies (not even standard, like categories and tags) in the search. The code searches in
post_title
andpost_content
, but if you want to include anything else you should hook into theposts_search
filter. -
- 2013-09-08
Hoprovato la soluzione di Onetrickpony sopra https://wordpress.stackexchange.com/a/5404/37612,che èfantastico,ma hotrovato unproblema lì,chenon hafunzionatoperme,e vorrei apportare unapiccolamodifica:
- se ho cercato una stringaneltitolo dellatassonomia,funzionabenissimo
-
se latassonomia ha caratteri speciali,ades. con "Umlauts"tedeschi (ö,ä,ü)e si cerca oe,ae,ueinvece di usareil carattere speciale - ènecessario aggiungere la ricercanello slug dellatassonomia -
OR t.slug LIKE '%".get_search_query()."%'
-
se cerchi una combinazione di una query di ricercae unfiltro ditassonomia,anche questofunzionabene
-
Mailproblema è che quando sitenta di utilizzare soloilfiltro dellatassonomia: l'hook di ricerca aggiunge una stringa vuota alla query senon viene cercato alcuntestoe per questomotivo si ottengono TUTTIi postnel risultatoinvece di solo quelli dellatassonomiafiltrata. Una sempliceistruzione IF risolveilproblema. Quindi l'intero codicemodificato sarebbe questo (funzionaperfettamenteperme!)
funzione custom_search_where ($ where) { global $ wpdb; if (is_search () &&get_search_query ()) $ where.="OR ((t.name LIKE '%".get_search_query (). "%' ORt.slug LIKE '%".get_search_query (). "%') AND {$ wpdb->posts} .post_status='pubblica') "; return $ dove; } funzione custom_search_join ($join) { global $ wpdb; if (is_search () &&get_search_query ()) $join.="LEFT JOIN {$ wpdb->term_relationships}tr ON {$ wpdb->posts} .ID=tr.object_id INNER JOIN {$ wpdb->term_taxonomy}tt ONtt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN { $ wpdb->termini}t ONt.term_id=tt.term_id "; return $join; } funzione custom_search_groupby ($groupby) { global $ wpdb; //dobbiamo raggruppare sull'ID delpost $groupby_id="{$ wpdb->posts} .ID"; if (!is_search ()|| strpos ($groupby,$groupby_id)!==false||!get_search_query ()) return $groupby; //groupbyera vuoto,usailnostro if (! strlen (trim ($groupby))) restituisce $groupby_id; //nonera vuoto,aggiungiilnostro return $groupby. ",". $groupby_id; } add_filter ('posts_where','custom_search_where'); add_filter ('posts_join','custom_search_join'); add_filter ('posts_groupby','custom_search_groupby');
I tried the solution of Onetrickpony above https://wordpress.stackexchange.com/a/5404/37612, which is great, but I found one issue there, which did not work for me, and I would make one small modification:
- if I searched for a string in the title of the taxonomy - it works great
if the taxonomy has special characters e.g. with german "Umlauts" (ö,ä,ü) and one searches for oe, ae, ue insteda of using the special char - you need to add the search in the slug of the taxonomy -
OR t.slug LIKE '%".get_search_query()."%'
if you search for a combination of a search query and a taxonomy filter - this also works fine
But the problem is, when you try to use only the taxonomy filter - the search hook append an empty string to the query if no text is searched for, and for that reason you get ALL posts in the result, instead of only those from the filtered taxonomy. A simple IF statement solves the problem. So the whole modified code would be this (works perfectly fine for me!)
function custom_search_where($where){ global $wpdb; if (is_search() && get_search_query()) $where .= "OR ((t.name LIKE '%".get_search_query()."%' OR t.slug LIKE '%".get_search_query()."%') AND {$wpdb->posts}.post_status = 'publish')"; return $where; } function custom_search_join($join){ global $wpdb; if (is_search()&& get_search_query()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function custom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false || !get_search_query()) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','custom_search_where'); add_filter('posts_join', 'custom_search_join'); add_filter('posts_groupby', 'custom_search_groupby');
-
- 2010-11-06
Ho lo stesso livello diinformazioni come Jan. So che èpossibileestendere la ricerca anche coni plugin.
Probabilmente Search Everything (Wordpress Plugin) è quello che stai cercando.Secondo l'elenco delle caratteristiche,ora supportatassonomiepersonalizzate.
I have the same level of information like Jan. I know it's possible to extend search with plugins as well.
Probably Search Everything (Wordpress Plugin) is what you are looking for. According to the feature list, it now supports custom taxonomies.
-
+1perilplug-in Cercatutto.Funziona comeprevistoe restituiscepiù risultati rispetto alla ricerca standard di Wordpress.+1 For Search Everything plugin. It works as expected and returns more results than standard Wordpress search.
- 0
- 2010-12-02
- PNMG
-
- 2012-12-08
Ho lo stessoproblema conilplug-in del carrello di WooCommerce. I risultati dellamia ricercanonincludonoiltermine dellatassonomiapersonalizzata,"tag_prodotto",perchénon è untagpost standard.Hotrovato una soluzionein questo altrothread StackOverflow sull'argomento:
L'esempio di codice di tkelly hafunzionatoperme quando ho sostituitoiltermine
author
nel suoesempioconproduct_tag
secondo lenostreesigenzeperi plug-in del carrello.I have the same problem going on with the WooCommerce cart plugin.. My search results are not including the custom taxonomy term, 'product_tag', because it not a standard post tag. I found a solution in this other StackOverflow thread about the matter:
The code example by tkelly worked for me when replacing the term
author
in his example withproduct_tag
as per our needs for the cart plugins. -
- 2015-11-12
Hotrovato la risposta di onetrickpony ottima,matratta qualsiasi ricerca come un singoloterminee inoltrenontratta unafrase di ricerca racchiusatra virgolette. Homodificato unpo 'il suo codice (inparticolare,lafunzione
atom_search_where
)per affrontare queste due situazioni. Ecco lamia versionemodificata del suo codice:// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb, $wp_query; if (is_search()) { $search_terms = get_query_var( 'search_terms' ); $where .= " OR ("; $i = 0; foreach ($search_terms as $search_term) { $i++; if ($i>1) $where .= " AND"; // --- make this OR if you prefer not requiring all search terms to match taxonomies $where .= " (t.name LIKE '%".$search_term."%')"; } $where .= " AND {$wpdb->posts}.post_status = 'publish')"; } return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
I found the answer from onetrickpony to be great but it treats any search as a single term and also won't deal with a search phrase enclosed with quotation marks. I modified his code (specifically, the
atom_search_where
function) a bit to deal with these two situations. Here is my modified version of his code:// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23 function atom_search_where($where){ global $wpdb, $wp_query; if (is_search()) { $search_terms = get_query_var( 'search_terms' ); $where .= " OR ("; $i = 0; foreach ($search_terms as $search_term) { $i++; if ($i>1) $where .= " AND"; // --- make this OR if you prefer not requiring all search terms to match taxonomies $where .= " (t.name LIKE '%".$search_term."%')"; } $where .= " AND {$wpdb->posts}.post_status = 'publish')"; } return $where; } function atom_search_join($join){ global $wpdb; if (is_search()) $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; return $join; } function atom_search_groupby($groupby){ global $wpdb; // we need to group on post ID $groupby_id = "{$wpdb->posts}.ID"; if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; // groupby was empty, use ours if(!strlen(trim($groupby))) return $groupby_id; // wasn't empty, append ours return $groupby.", ".$groupby_id; } add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); add_filter('posts_groupby', 'atom_search_groupby');
Ho duetassonomiepersonalizzate applicate a duetipi dipostpersonalizzati.l'elenco deitermininellabarra laterale vabene eelencheràtuttii post adesso associati.Tuttavia,se cerchi uno deiterminiin modo specifico,non viene visualizzato unpost con queltermine.
Esempio: http://dev.andrewnorcross.com/das/all-case-studies/ Cercailtermine "PQRI"
Non ricevonulla.Qualcheidea?Hoprovato a utilizzare variplugin di ricercamainterromponoi mieiparametri di ricercapersonalizzati o semplicementenonfunzionano.