I want to query a wordpress database in order to get all the desired posts alongside with their images. I have managed to do it with 2 different queries. The first one, brings all the posts alongside with the desired fields and the second one, runs in a foreach loop in order to get the image for each post. My problem is that this takes a really long time and i want to avoid id. I need a solution in order to avoid the foreach loop in the second query and merge it into the first.
Query 1
"SELECT * FROM wp_posts";
And foreach returned posts.ID
Query 2
"SELECT wp_posts.guid IN (Select wp_postmeta.meta_value from wp_postmeta where wp_postmeta.meta_key = '_thumbnail_id' AND wp_postmeta.post_id = ('the returned post id foreach of ID output of the first query')"