I’ve stored some serialized data in wp-postmeta table, e.g.:
$data = array(
'details' => $dettagli,
'vernice' => $vernice,
'reperibile' => $reperibile,
'valore2' => $valore2,
'valuta2' => $valuta2,
'subcat' => $subcat
);
add_post_meta($post_ID, 'meta', $data);
I would like to extract all the posts with the key “details” and value = “4”. How can I do it?
I’ve tried doing this code:
$args = array(
'post_type' => 'custom-post-type',
'posts_per_page' => -1,
'meta_query' =>
array(
'key' => 'details',
'value' => '4',
'compare' => 'LIKE'
),
'meta_key' => 'meta',
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
the_ID();
}
}
What is wrong?
meta_query
is an array of arrays.You might try this:
however i’m not 100% sure if this could help you, let’s try it, please let me know if you get stuck.
UPDATE
Serialized array in post meta are not good deals. So i suggest you to change your save function to store custom fields individually.
I’ve found a way to unserialize your previous post meta values:
Now you should be able to query as you want.
Hope it helps!
Have you tried this .Just a try