Search in metavalue which is an array

I try to do a personnalised search engine on my wordpress.
So i want user can search for all advanced custom field. I can do it for simple field which have just one value but if the text field have multiple value separated by commas i can’t do it.

I have somathing like that for my query:

Read More
SELECT DISTINCT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3, wp_postmeta wpostmeta4 
WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND
      wposts.ID = wpostmeta3.post_id AND wposts.ID = wpostmeta4.post_id AND 
      wpostmeta.meta_key = 'ville_ou_se_deroulera_la_colonie' AND
      lyon IN wp_postmeta.meta_value  AND wposts.post_status = 'publish' AND
      wposts.post_type = 'post' 
ORDER BY wposts.post_date DESC 

Is there something to do for checking inside a field which have multiple value ?

EDIT : i tried the FIND_IN_SET function like that but no result :

SELECT DISTINCT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3, wp_postmeta wpostmeta4
WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND
      wposts.ID = wpostmeta3.post_id AND wposts.ID = wpostmeta4.post_id AND
      wpostmeta.meta_key = 'ville_ou_se_deroulera_la_colonie' AND
      FIND_IN_SET('lyon', wpostmeta.meta_value) AND
      wposts.post_status = 'publish' AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC 

Related posts

Leave a Reply

1 comment

  1. You should probably use LIKE or REGEXP to search the field – try:

    SELECT DISTINCT wposts.*
    FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3, wp_postmeta wpostmeta4
    WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND
          wposts.ID = wpostmeta3.post_id AND wposts.ID = wpostmeta4.post_id AND
          wpostmeta.meta_key = 'ville_ou_se_deroulera_la_colonie' AND
          wpostmeta.meta_value REGEXP '^lyon[,]|[,]lyon[,]|[,]lyon$|^lyon$' AND
          wposts.post_status = 'publish' AND wposts.post_type = 'post'
    ORDER BY wposts.post_date DESC
    

    This should find any of the following

    lyon
    lyon,bordeaux
    la rochelle,lyon
    la rochelle,lyon,bordeaux
    

    but should not match:

    la rochelle,lyons,bordeaux
    

    i.e. it should not matter if there are comments there or not as long as “lyon” is there on its own