• Resolved tazar64

    (@tazar64)


    Hello,

    I try to add to a relationship field between User extended and a Course POD, using the code below and it is not working. The full code and other functionalities are well tested – only updating/adding more to this relationship is not. Could you please help?

        $user_pod = pods('user', $student_id);
        foreach ($course_ids as $course_id) {
            $user_pod->add_to('acces_user_exp', $course_id);
        }

    acces_user_exp is a relationship field with courses. (with one ‘s’)

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tazar64

    Are you sure you apply this in the right direction?
    Based on the meta name it seems that you need to add the user to the course, not the course to the user, correct?

    Cheers, Jory

    Thread Starter tazar64

    (@tazar64)

    Thank you for your response. Unfortunately, it does not work the other way around either. The code now is:

    add_action(‘wpcw_enroll_student’, function($student_id, $course_ids) {
    if (is_array($course_ids) && !empty($course_ids)) {
    foreach ($course_ids as $course_id) {
    $course_pod = pods(‘wpcw_course’, $course_id);
    $course_pod->add_to(‘acces_user_exp-back’, $student_id);
    }
    }
    }, 10, 2);

    I have tested by updating other fields, and that worked fine. Could you please advise what might be wrong?

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tazar64

    I see you have also changed the field name. Could you share your field configurations? Or at least point out what field belongs to which Pod?

    Cheers, Jory

    Thread Starter tazar64

    (@tazar64)

    The User extended POD has the relationship field: acces_user_exp and
    wpcw_course POD has the bi-directional field: acces_user_exp-back

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tazar64

    I’ve checked the code and your initial config should work fine.
    Though the add_to method also accepts a list of ID’s.

    $user_pod->add_to( 'acces_user_exp', $course_ids );

    Please make sure that the contents of $course_ids are in fact a list of ID’s, and not something else, like objects or an array of object vars.

    Cheers, Jory

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add to Relationship’ is closed to new replies.