getting posts and number by specific meta value in multiple meta

When you store an array as meta_value you are storing a string with serialized data that represents the original array. This format is appropriated for data that are only intended to display.

Data that you plan to use for ordering or searching by or in any other way that is not just display the information should NOT be stored as serialized array. You should store each array key in its own meta field.

In you example you want to search post by artist meta field, so think about the change from array artist[1599, 5240] to two single value meta fields (all with “artist” as meta_key, you can store a key more than once):

[artist] => ‘1599’
[artist] => ‘5240’


Update

it’s serialized and must search in serialized, how do it ?

It is technically possible to search text with the LIKE operator and surrounding value with ", but it’s inefficient query. You have no guarantee of the results consistent, because searched value can appear in other array key (e.g. rev, master, etc.). Storing artist as separate meta fields is a preferable solution.

'meta_query' => array(
    array(
        'key'     => 'cp_meta',
        'value'   => '"1599"', // OR  ':"1599"'
        'compare' => 'LIKE',
),

Leave a Comment