Meta query with JSON value

Im using post meta to store some value. Im having the following in wp_postmeta table:

meta_key

mykey

meta_value

a:2:{i:0;s:1:"5";i:1;s:1:"2";}

I need to return posts which have meta_value of 2.

My query:

$args = array(
               'post_type' = 'post',
               'meta_key' = 'mykey',
               'orderby' = 'meta_value_num',
               'order' = 'ASC',
               'meta_query' = array(
                   array(
                       'key' = 'mykey',
                       'value' = '2',
                       'compare' = 'IN',
                   )
               )
             );
$the_query = new WP_Query($args);

But no posts returned... Where i made mistake?

Topic meta-query wp-query Wordpress

Category Web


That's not json. A semi-colon won't work inside a javaScript Object like that.

That's serialized-data. If you return that string & then call unserialize on it & then var_dump you can see what's inside.

Also, see fuxia♦'s Answer Here for more info.

& to Maikal's credit, you can use a meta_query in this situation, but you have to use the correct syntax...

I believe, it's:

'compare' => 'LIKE', 
'value' => '"2"'

& Darren Felton's Answer Here seems to verify that

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.