Home Forums ACF PRO Checking if Custom Post Type already Exists


Checking if Custom Post Type already Exists

  • Hello, I have a CPT called certificate which in turn has an custom field called Student Name and another called Course Taken. The Student name field is a normal text field and the Course Taken is an Post Object Field that selects from another CPT.

    I would like to check if a student has already taken a course before creating a new certificate, via the front end form. So, if the user creates a certificate via acf_form I would like to display a validation error that says the certificate already exists, and maybe a link to the certificate.

    I am trying to use the acf/validate_save_post to check if the certificate with the fields already exist. But I do not know how to access this fields within the function call.

  • Hi @juanmtorrijos

    I believe you can check if a student has a certificate or not like this:

    // Get the posted data
    $posted_course = $_POST['acf']['field_1234567890abc'];
    $student_name = $_POST['acf']['field_abcdefghij123'];
    // Check if a certificate with the same name and course exsists or not
    $the_posts = get_posts(array(
        'post_type' => 'certificate',
        'meta_query' => array(
            'relation' => 'AND',
                'key' => 'student_name_field',
                'value' => $student_name
                'compare' => '=',
                'key' => 'course_field',
                'value' => $posted_course,
                'compare' => '=',
    if( $the_post ){
        // The student has taken the course. Give a warning here
    } else {
        // The student hasn't taken the course yet. Process it here.

    Where ‘field_1234567890abc’ and ‘field_abcdefghij123’ is the field keys.

    I hope this helps 🙂

  • Hi James,

    Yes I have this, however I don’t know how to return the error message to the page.

    I tried setting $GLOBALS[‘acf_validation_errors’] to a string inside the if ($the_post), but it simply doesn’t work. I also tried setting it to an array with one string.

    I’m using acf/validate_save_post filter. Should I use acf/validate_value

  • Shameless plug – you can use my Validated Field plugin to check ACF field submissions. I am currently working on an upgrade (done with 5.x, working through the 4.x code now) but you can check it out in the WordPress repo here:

    The logic above would still be used as part of the PHP validation, and if there is an error you would just return "this is the error message"; otherwise you can return true; to have it pass (or just do nothing, pass is the default).

  • If you don’t want to use my plugin, just hook your logic into one of the following validate filters – acf/validate_value, acf/validate_value/type={$field['type']}, acf/validate_value/name={$field['name']}, or acf/validate_value/key={$field['key']}. These filters take the arguments $valid, $value, $field, $input and return true for success or a string if there was an error.

    Also let me know if you would like to try out the new version of my validation plugin – it might contain a few bugs still but has a lot of new features.

  • Hi @jsilver,

    The validate_value hook works perfectly. I am using that for now, but I did install your plugin and I will give it a try.

    The error message output is not really super user friendly using the validate_value hoop. I want to have more control over the error message output.

    Thank you very much!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Checking if Custom Post Type already Exists’ is closed to new replies.