WP_Query meta_query by array key

You can’t do that. When you save postmeta (or any other meta, for that matter), WordPress runs it through maybe_serialize which turns objects and arrays into serialized data. When it gets pulled back out, it is run through maybe_unserialize.

Serialized data looks something like this:

a:1:{s:3:"one";s:3:"two";}

In other words, what get’s stored cannot be queried in the way you want. The best you could do is a LIKE query, which will be unreliable at best and less performant. Just use LIKE as the compare argument of your meta query.

Your best bet: rethink how you store meta and move whatever key is to its own entry. Meta tables in WordPress are key value stores, it’s not unreasonable to store multiple meta values per post type or plugin.

Leave a Comment