You don’t need to set a new WP_Query since you are looping through the data of the octopus. I would do the following in your case:
Create a function that checks if there is a post with an octopus_id given
function post_with_octopus_id_exists( $octopus_id ){
//If for some reason the $octopus_id given is empty return false
if( empty( $octopus_id ) ){
return false;
}
//Do a meta query to fetch posts with the given $octopus_id
$args = array(
'post_type' => 'employee',
'post_status' => 'any'
'meta_query' => array(
'relation' => AND,
array(
'key' => '_octopus_id',
'compare' => 'EXISTS'
),
array(
'key' => '_octopus_id',
'value' => $octopus_id,
'compare' => '=',
)
)
);
$the_query = new WP_Query( $args );
//if there are posts with the given $octopus_id return true otherwise return false
return ( $the_query->have_posts() ? true : false );
}
Then inside your loop you can call the function as following:
foreach ( $records->data as $record ) {
if( ! post_with_octopus_id_exists( $record->id ) ){
echo "Hello";
}
}
Important: Make sure that the meta_key stored for each post is named as “_octopus_id”