I have a script that looks like this:
foreach($target_zips as $zipcode) {
$query_adresses = array (
'order' => 'ASC',
'cat' => $_GET["cat"],
'post_type'=> 'adressen',
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => 'postcode',
'value' => $zipcode,
'compare' => 'LIKE',
)
)
);
$results = get_posts( $query_adresses );
$matched_adresses[] = $results;
}
A script that goes through an array of values, then queries a lot of posts to see if there’s posts that match meta_query. This is terribly slow. Is it possible to put an array of values in this meta_query instead of querying over and over again for each value in $target_zips
?
You can run just one get_posts() query that will return all the results. Build the meta_query beforehand.
See this for for more details: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
C. Smiths solution is correct but there is a simpler solution.
You can utilize the IN clause in your compare key. Set the value key to an array of values that need to be compared.
More info and examples can be found here.
https://developer.wordpress.org/reference/classes/wp_meta_query/#accepted-arguments
Edit
Note
Like sMyles suggested, you MUST validate/sanitize your external data ($_GET)