我有一个看起来像这样的脚本:
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;
}
一个遍历一系列值的脚本,然后查询很多帖子以查看是否有与meta_query匹配的帖子。这非常慢。是否可以在此meta_query中放置一组值,而不是一遍又一遍地查询$target_zips
中的每个值?
答案 0 :(得分:0)
您只能运行一个get_posts()查询,该查询将返回所有结果。预先构建meta_query。
$meta_query = [];
/**
* This will return all posts that contain at least one meta_query from the loop
*/
$meta_query['relation'] = 'OR';
foreach ( $target_zips as $zipcode ) {
$meta_query[] = [
'key' => 'postcode',
'value' => $zipcode,
'compare' => 'LIKE',
];
}
$query_adresses = [
'order' => 'ASC',
'cat' => $_GET["cat"],
'post_type' => 'adressen',
'posts_per_page' => '-1',
'meta_query' => $meta_query
];
$matched_adresses = get_posts( $query_adresses );
有关更多详细信息,请参见此:https://codex.wordpress.org/Class_Reference/WP_Meta_Query