meta_query 'compare' => 'IN' non funziona
-
-
è serializzato `system_power_supply`?is `system_power_supply` serialized?
- 0
- 2015-04-03
- Howdy_McGee
-
sì.Eccoil valorenellatabellapostmeta `a: 1: {i: 0; s: 6:" single ";}`yes, it is. Here's the value in postmeta table `a:1:{i:0;s:6:"single";}`
- 0
- 2015-04-03
- Rizwan
-
beh questo èiltuoproblema,lameta querynonfunzionerà sui dati serializzati.se cerchiin questo sitoperinterrogare dati serializzatitroverai alcune risposte,manessunmetodo sarà l'ideale.well that's your problem, meta query won't work on serialized data. if you search this site for querying serialized data you'll find some answers, but no method will be ideal.
- 2
- 2015-04-03
- Milo
-
2 risposta
- voti
-
- 2015-04-03
Nonesiste unmodo sempliceper cercare valori serializzatiin unameta query.Se l'elenco dei valorinon ètroppo lungo,potenzialmentepotrestiimpostarepiùmeta query:
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'system_power_supply', 'value' => 'single', 'compare' => 'LIKE', ), array( 'key' => 'system_power_supply', 'value' => 'redundant', 'compare' => 'LIKE', ) )
Oppure,se vuoi diventare superfantasioso,puoiimpostarlo dinamicamente:
$values_to_search = array('single', 'redundant'); $meta_query = array('relation' => 'OR'); foreach ($values_to_search as $value) { $meta_query[] = array( 'key' => 'system_power_supply', 'value' => $value, 'compare' => 'LIKE', ); }
There's no easy way to search serialized values in a meta query. If the list of values isn't crazy long, potentially you could set up multiple meta queries:
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'system_power_supply', 'value' => 'single', 'compare' => 'LIKE', ), array( 'key' => 'system_power_supply', 'value' => 'redundant', 'compare' => 'LIKE', ) )
Or if you wanted to get super fancy, you could set it up dynamically:
$values_to_search = array('single', 'redundant'); $meta_query = array('relation' => 'OR'); foreach ($values_to_search as $value) { $meta_query[] = array( 'key' => 'system_power_supply', 'value' => $value, 'compare' => 'LIKE', ); }
-
Graziemille,fratello.Nonposso dirti quantograndemal ditesta hai risoltoilmio.Thank you so much, bro. I can't tell you how big headache you've solved of mine.
- 0
- 2015-04-03
- Rizwan
-
btw,perchéilmeta_value contenente dati serializzati senon siamoin grado diinterrogaretramitemeta_query?È unbug di wordpress?btw, why the meta_value containing serialized data if we're not able to query through meta_query ? Is this wordpress's bug?
- 0
- 2015-04-03
- Rizwan
-
Sono una donna,non un "fratello",manessunproblema.Ilmeta_value contiene dati serializzati a causa delmodoin cui Advanced Custom Fields salvai dati.Non è l'ideale,di sicuro.I'm a woman, not a "bro", but no problem. The meta_value contains serialized data because of the way Advanced Custom Fields saves the data. It's not ideal, for sure.
- 1
- 2015-04-06
- Jen
-
Aproposito,quale soluzione hafunzionatoperte?BTW, which solution worked for you?
- 0
- 2015-04-06
- Jen
-
haha,chiedo scusa allamia signora.Il secondoe ilterzo hannofunzionatobene,non hoprovatoilprimo.haha, I apologize my lady. Second and third worked well, didn't try the first one.
- 1
- 2015-04-06
- Rizwan
-
È raro vedere unaprogrammatrice signora,chenonmorde affatto: DIt's unusual to see a lady programmer, who don't bite at all :D
- 0
- 2015-04-06
- Rizwan
-
puoi rimuovereilprimo,nonfunzionayou can remove the first one, it doesn't work
- 2
- 2016-03-12
- Toskan
-
- 2017-12-21
So che èpassatomoltotempo,manel caso qualcuno avesse lo stessoproblema.Beh,mi sonotiratoi capelliper oreprima ditrovareilproblema: "meta_query" con l'operatore di confronto "IN"non sembra accettareil solito array.invece,deviprima unirti con ",".
Quindi,neltuo caso,qualcosa delgenere dovrebbefunzionare:
$args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'system_power_supply', 'value' => join(', ', array('single', 'redundant')), 'compare' => 'IN', ) ) ); $query = new WP_Query($args); echo $query->found_posts;
I know it's been a long time, but just in case someone has the same issue. Well i've been pulling my hair for hours before i found the issue: 'meta_query' with 'IN' comparison operator doesn't seem to accept the usual array. instead, you need to join it first with ', '.
So, in your case, something like this should work :
$args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'system_power_supply', 'value' => join(', ', array('single', 'redundant')), 'compare' => 'IN', ) ) ); $query = new WP_Query($args); echo $query->found_posts;
-
In WP 5puoi semplicementepassare l'array direttamente alla chiave del valore.Seimplodein una stringa,potresti ottenere risultatiinaspettati riguardo a come wp divide la stringain segmentiper laparte "IN ()".Adesempio,"questo quello","e","quello" diventa "questo","quello","e","quello",quindi sembraesseremeglio dargli semplicemente l'array.In WP 5 you can just pass the array directly to the value key. If you implode it to a string you may get unexpected results in regards to how wp splits the string up into segments for the `IN()` part. Eg, `'this that', 'and', 'that'` becomes `'this','that','and','that'` - so seems to be better to just give it the array.
- 0
- 2019-05-03
- Bananaapple
Prima ditutto,so che è un duplicato,manessuna delle risposteprecedenti è stata utile.
Sto cercandonei posttramite
post_meta
. Eccoilmio codice,che almomentonon restituiscenulla.Se rimuovo
meta_query
funziona. Sono sicuro di queste cose:key
onelvalue
.post