meta_query 'compare' => 'IN' ne fonctionne pas
-
-
`system_power_supply`est-il sérialisé?is `system_power_supply` serialized?
- 0
- 2015-04-03
- Howdy_McGee
-
Oui,ça l'est.Voici la valeur dans latablepostmeta `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
-
Ehbien,c'est votreproblème,laméta-requêtene fonctionnerapas sur les données sérialisées.si vous recherchez sur ce site des requêtes de données sérialisées,voustrouverez des réponses,mais aucuneméthodene seraidéale.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 réponses
- votes
-
- 2015-04-03
Iln'y apas demoyen simple de rechercher des valeurs sérialisées dans uneméta-requête.Si la liste de valeursn'estpastrop longue,vouspouvezpotentiellement configurerplusieursméta-requêtes:
'meta_query' => array( 'relation' => 'OR', array( 'key' => 'system_power_supply', 'value' => 'single', 'compare' => 'LIKE', ), array( 'key' => 'system_power_supply', 'value' => 'redundant', 'compare' => 'LIKE', ) )
Ou si vous voulez être super sophistiqué,vouspouvez le configurer dynamiquement:
$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', ); }
-
Mercibeaucoup,monfrère.Jene peuxpas vous dire à quelpoint vous avez résolu lemal detête dumien.Thank you so much, bro. I can't tell you how big headache you've solved of mine.
- 0
- 2015-04-03
- Rizwan
-
btw,pourquoi lameta_value contenant des données sérialisées sinousne pouvonspasinterroger viameta_query?Est-ce lebug de 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
-
Je suis unefemme,pas un "frère",maispas deproblème.Lameta_value contient des données sérialiséesen raison de lamanière dont Advanced Custom Fieldsenregistre les données.Cen'estpasidéal,bien sûr.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
-
BTW,quelle solution afonctionnépour vous?BTW, which solution worked for you?
- 0
- 2015-04-06
- Jen
-
haha,je m'excusema dame.Les deuxièmeet troisième ontbien fonctionné,je n'aipasessayé lepremier.haha, I apologize my lady. Second and third worked well, didn't try the first one.
- 1
- 2015-04-06
- Rizwan
-
Ilestinhabituel de voir uneprogrammeuse,quine mordpas dutout: DIt's unusual to see a lady programmer, who don't bite at all :D
- 0
- 2015-04-06
- Rizwan
-
vouspouvez supprimer lepremier,celane fonctionnepasyou can remove the first one, it doesn't work
- 2
- 2016-03-12
- Toskan
-
- 2017-12-21
Je sais que çafait longtemps,maisjuste au cas où quelqu'un aurait lemêmeproblème.Ehbien,je me suistiré les cheveuxpendant des heures avant detrouver leproblème: 'meta_query' avec l'opérateur de comparaison 'IN'ne semblepas accepter letableau habituel.à laplace,vous devez d'abord le rejoindre avec ",".
Donc,dans votre cas,quelque chose comme ça devraitfonctionner:
$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;
-
Dans WP 5,vouspouvez simplementpasser letableau directement à la clé de valeur.Si vous l'implosezen une chaîne,vouspouvez obtenir des résultatsinattendusen ce qui concerne lafaçon dont wp divise la chaîneen segmentspour lapartie `IN ()`.Parexemple,"" ceci cela ","et "," cela "devient" ceci "," cela ","et "cela",il semble doncpréférable de lui donner simplement letableau.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
Je recherche dans lesmessages via
post_meta
. Voicimon code,quine renvoie actuellement rien.Sije supprime
meta_query
,celafonctionne. Je suis sûr de ces choses:key
ou lavalue
.post