【发布时间】:2019-10-15 11:24:18
【问题描述】:
$wp_query = new WP_Query([
'post_type' => 'office',
'post_status' => 'any',
'posts_per_page' => -1,
]);
$offices = [];
if (count($wp_query->posts) > 0) {
$offices = $wp_query->posts;
}
$keys = [];
foreach ($offices as $office) {
$keys[] = get_post_meta($office->ID, '_office_id', true);
}
foreach ($records as $record) {
$record_key = strtoupper(str_replace(' ', '',
trim($record->location_id) . trim($record->business_unit)));
if (isset($keys) & !empty($keys)) {
if (in_array($record_key, $keys)) {
echo $record_key . ' has a record in WordPress.<br/>';
} else {
echo '<b>' . $record_key . ' doesnt have a record in WordPress.</b><br/>';
}
}
}
这是我目前使用上述代码得到的,运行良好:
MONTREALFHTOR 在 WordPress 中有记录。
MPLSFHUSA 在 WordPress 中没有记录。
NEWYORKFHUSA 在 WordPress 中有记录。
ORANGECO.FHUSA 在 WordPress 中没有记录。
但上面的代码将遍历所有 $keys 超过 100 次,直到它匹配所有 $record_key - 这是我想要实现的目标:
-- While loop over offices -- In the loop, for each office, get post meta location ID and business unit and put together as new key -- Loop through the offices one time, and build an array -- Use that array to loop through $records once to match the $record_key and $keys
【问题讨论】: