In case anyone’s wondering I managed to get this fixed with a pointer about using var_dump(). Here’s the modified loop.
function build_home_id ($pcode_id, $assigned_id_no) {
//$cmplt_id = $pcode_id . "-" . number_pad($assigned_id_no, 3); // Gets postcode string and calls the number padding function to add the leading 0's
do {
$cmplt_id = $pcode_id . "-" . number_pad($assigned_id_no, 3); // Gets postcode string and calls the number padding function to add the leading 0's
$assigned_id_no++; // Increase assigned_id_no
$existing = get_page_by_title($cmplt_id, OBJECT, "patient");
$existing_post = $existing->post_title;
}
while($cmplt_id == $existing_post); // Restart loop if built id matches any already in existance
return $cmplt_id;
}
The problem is the get_page_by_title() returns an object not a string. Hope this helps someone.