2

i have a multiple meta (custom field) in posts as "cd_meta" that it (cd_meta) have a meta as "artist", also "artist" meta maybe have one or more value in array

i want to getting posts that "artist" meta have specific value and number of them

look below picture for more info enter image description here

4
  • How do you store cd_meta? Is it a serialized array with keys artist, video_type, etc., or each key (artist, video_type) is a separate custom field?
    – nmr
    Aug 28, 2018 at 15:11
  • @nmr , i think serialized and not separate. from above picture artist, video_type and .. are in cd_meta Aug 28, 2018 at 20:13
  • probably, but I was not sure that it was not processed in PHP
    – nmr
    Aug 28, 2018 at 20:32
  • its serialized and must search in serialized , how do it ? Aug 29, 2018 at 7:26

2 Answers 2

1

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',
),
0

Please change your SQL Query

$args = array(
   'meta_query' => array(
       array(
           'key' => 'cp_annonceur',
           'value' => 'professionnel',
           'compare' => '=',
       )
   )
);
$query = new WP_Query($args);
2
  • My Code link Aug 28, 2018 at 12:32
  • 1
    Please don't post code as a picture. Edit the question and paste the code. @ebeliejinfren
    – nmr
    Aug 28, 2018 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.