I need to make custom code for a Wordpress project. I need to get the product gallery.
This is the query to get all the ID's of the productimages of that product. This returns a string like for example "212, 213, 214".
SELECT meta_value FROM `wp_postmeta` WHERE meta_key='_product_image_gallery' AND post_id=?
Then I use it as a subquery like this:
SELECT guid FROM wp_posts WHERE ID IN (SELECT meta_value FROM `wp_postmeta` WHERE meta_key='_product_image_gallery' AND post_id=?)
The problem is that the WHERE ID IN
is not working because the subquery returns a string of the ID's and does not view the ID's as separate instances. This is because all the productimages ID's are stored in a single cell.
How do I fix this? Is there some kind of split()
function?
TL;DR
I need to convert
SELECT guid FROM wp_posts WHERE ID IN ("213, 214, 215")
to
SELECT guid FROM wp_posts WHERE ID IN (213, 214, 215)
knowing that "213, 214, 215"
is retrieved from a subquery.