You can choose between simplicity and efficiency.
Running a get_posts()
with a meta query for every entry is the simplest approach, and will do the job. Of course, as you pointed out, there are performance considerations.
If you are looking for a more efficient solution, you will have to dive as deep as the database layer and run your own SELECT
query. Then, store the results in an associative array and check every entry against that.
Something like the following:
global $wpdb; // if we're in a function
$q="
SELECT $wpdb->posts.ID,$wpdb->postmeta.meta_value
FROM $wpdb->posts
JOIN $wpdb->postmeta ON ($wpdb->posts.ID=$wpdb->postmeta.post_id)
WHERE $wpdb->post.post_type="event" and $wpdb->postmeta.meta_name="xml_id"
";
$rows=$wpdb->get_results($q,OBJECT);
$events_idx=array();
foreach($rows as $row) $events_idx[$row->meta_value]=$row->ID;
After that, checking if an event already exists is as easy as isset($events_idx[$xml_id])
. And if it eventually exists, you would update the post with id
given by $post_id=$events_idx[$xml_id]
.