Support

Account

Home Forums General Issues Update User Repeater with Post Object Info?

Solved

Update User Repeater with Post Object Info?

  • Hello,

    I’m running into some issues with a particular setup of ACF and was wondering if someone could weigh in with some info.

    Here’s my scenario:
    I have two Custom Post Types: “Online Courses” and “Course Points”.

    I’m using a WordPress online course plugin (LearnDash) that allows Users to complete courses; I then want them to receive a “Course Point”, housed within a repeater in their profile. I’ve added an ACF Post Object field into the Admin for each Online Course which allows an administrator to assign the Online Course a corresponding “Course Point”.

    So, I’m hooking into when a User completes a course and I’m adding a new repeater row to that Users’ profile, but I just can’t seem to get the Post Object Field for “Course Points” to get the data (from the Online Course) within the repeater. For example: there is an Online Course called “Test Course” and a Course Point called “Two Points”. When User completes “Test Course”, I want the repeater field in their profile to show that they have “Two Points”.

    My Code:

    add_action("learndash_course_completed", function($data) {
    	$user = get_current_user_id();
    	$user_ID = 'user_' . $user;
    	
    	//Select the Users' Repeater
    	$field_key = 'course_points';
    
    	//Find which course point was assigned to the Online Course
    	$course_id = $data->ID;
    	$coursePoint = get_field('field_56646f51fa48b', 'course_' . $course_id);
    
    	$value = get_field('course_point', $user_ID);
    	$value[] = array("post_object_field" => $coursePoint, "date" => "20151206");
    	update_field( $field_key, $value, $user_ID );
    }, 5, 1);

    Any insight you can provide is greatly appreciated!

  • Ok, so I figured out how to properly get the ID of the course that was completed:

    $course_id = $data['course']->ID

    So, how can I then take my course and insert it into a repeater (with a Post Object sub field)? Here’s what I have so far, which isn’t working:

    $course_id = $data['course']->ID;
    $coursePoint = get_field('field_56646f51fa48b', $course_id);
    $value = get_field('field_56644f11d62c3', $user_ID); 
    
    $value[] = array("field_566459158d2ab" => $coursePoint, "field_5664598f8d2ad" => "20151206"); //The second field in the Repeat (Date) gets into a new row fine
    
    update_field( $field_key, $value, $user_ID );

    For reference, the multidimensional array that is output by $data is structured like this:

    Array
    (
        [user] => WP_User Object
            (
                [data] => stdClass Object
                    (
                        [ID] => 30
                        [user_login] => testing
                        [user_pass] => $P$BQqHM2nDR2C3WB87v2gd83R0ZCtRug.
                        [user_nicename] => testing
                        [user_email] => testing@testsite.com
                        [user_url] =>
                        [user_registered] => 2015-11-23 15:59:37
                        [user_activation_key] =>
                        [user_status] => 0
                        [display_name] => James Test
                    )
    
                [ID] => 30
                [caps] => Array
                    (
                        [s2member_level4] => 1
                        [access_s2member_ccap_member] => 1
                    )
    
                [cap_key] => wp_lxkfgb_capabilities
                [roles] => Array
                    (
                        [0] => s2member_level4
                    )
    
                [allcaps] => Array
                    (
                        [read] => 1
                        [level_0] => 1
                        [access_s2member_level0] => 1
                        [access_s2member_level1] => 1
                        [access_s2member_level2] => 1
                        [access_s2member_level3] => 1
                        [access_s2member_level4] => 1
                        [s2member_level4] => 1
                        [access_s2member_ccap_member] => 1
                    )
    
                [filter] =>
            )
    
        [course] => WP_Post Object
            (
                [ID] => 280
                [post_author] => 1
                [post_date] => 2015-12-05 11:21:55
                [post_date_gmt] => 2015-12-05 16:21:55
                [post_content] => This is another course!
                [post_title] => Another Course
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] =>
                [post_name] => another-course
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2015-12-06 12:27:39
                [post_modified_gmt] => 2015-12-06 17:27:39
                [post_content_filtered] =>
                [post_parent] => 0
                [guid] => http://testsite.com/vra/?post_type=sfwd-courses&p=280
                [menu_order] => 0
                [post_type] => sfwd-courses
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )
    
        [progress] => Array
            (
                [280] => Array
                    (
                        [lessons] => Array
                            (
                                [316] => 1
                            )
    
                        [topics] => Array
                            (
                                [316] => Array
                                    (
                                        [327] => 1
                                    )
    
                            )
    
                        [completed] => 1
                        [total] => 1
                    )
    
            )
    
    )
  • Solved it!

    I found this post which lead me to an answer.

    Here’s my code:

    $course_id = $data['course']->ID; //280
    $coursePoint = get_field('field_56646f51fa48b', $course_id);
    
    //Get the ID of the Point that was was assign to the Course
    if( $coursePoint ): 
    	$post = $coursePoint;
    	setup_postdata( $post ); 
    	$selectedCourseID = $post->ID;
    	wp_reset_postdata();
    endif;
    
    $value = get_field('field_56644f11d62c3', $user_ID); 
    
    $value[] = array("field_566459158d2ab" => $selectedCourseID, "field_5664598f8d2ad" => "20151206");
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.