1

I'm trying to make a seemingly simple bit of php (it output's to a Gravity Forms field, but that's basically irrelevant).

I'm using Gravity Forms to create posts, which I can do easily, however I want each post to have a unique title (as this is how the user navigates).

I managed to get this working for registration by checking if the user registration id (also the post title in that situation) was already in use, like so:

{ // Builds complete user id function build_id ($pcode_id, $assigned_id_no) {

 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
    }
    while(username_exists($cmplt_id));  // Restart loop if username_exists is true
    return $cmplt_id;

return $cmplt_id;

}

// Takes a string and add's however many 0's (or other character placed there) to the left of the string function number_pad($number,$n) { return str_pad((int) $number,$n,"0",STR_PAD_LEFT); } }

$pcode_id is just a formatted post code as a string and $assigned_id_no is simply a loop number, passes 1 basically.

As you can see the while criteria is does this exist, but for the life of me I can't emulate this by simply checking post type.

The nearest I've come is get_page_by_title() and if it's null then finish else loop, but it doesn't work:

{ function home_patient_id() { $post_id = $_GET['home_id']; $home_post_code = get_post_meta($post_id, "wpcf-home-post-code", true); $home_post_code = format_id($home_post_code); $home_post_code = build_home_id($home_post_code, 1);

return $home_post_code;

}

// Builds complete user id function build_home_id ($pcode_id, $assigned_id_no) {

 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
         $exist = get_page_by_title($cmplt_id);
        }
    while(get_page_by_title($cmplt_id));    // Restart loop if username_exists is true
    return $cmplt_id;

return $cmplt_id;

} }

I've also tried checking if it returns null but nothing is working, I either get nothing or an infinite loop.

Can someone please help I'm pulling my hair out over this!

Many thanks.

1

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.