Support

Account

Home Forums ACF PRO Checking if Custom Post Type already Exists Reply To: Checking if Custom Post Type already Exists

  • 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',
            array(
                'key' => 'student_name_field',
                'value' => $student_name
                'compare' => '=',
            ),
            array(
                '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 🙂