Check for missing post id’s

$qry = “SELECT p.ID FROM nbbuq_posts p WHERE p.post_type = ‘ad’ group by p.ID” ;
$psts = $wpdb->get_results( $qry );

Loop through post id

foreach($posts as $post){
$id = $post->ID;
$qry1 = “SELECT * FROM **_postmeta where post_id=”.$id.” AND meta_key=’ad_featured'”;
$psts1 = $wpdb->get_results( $qry1 );
if(isset($posts1[0]->meta_key))
echo $post->ID. ‘ FOUND’;
else
{
echo $post->ID. ‘ NOTĀ FOUND’;

//INSERT NEW META KEY AND VALUE
$wpdb->insert(‘**_postmeta’, array(
‘post_id’ => $id,
‘meta_key’ => ‘ad_featured’,
‘meta_value’ => ‘no’
));

}
}