Support

Account

Home Forums General Issues get_field not working in pre_get_posts

Solving

get_field not working in pre_get_posts

  • I am working on Learndash Based Website which is having Membership and we have given parents a way to show only specific category of quiz. And our code work fine when page loads happens normally.

    But, When user use Ajax button to load next page content then get_field is not working inside of our code and hence we are not able to get the values stored in option. We are not getting any errors. Our Code is not moving ahead after get_field and hence we are not even seeing any error_log which we are using to debug.

    Here is our code:

    if (!class_exists('NtmlConfigurePopupQuizEnable')) {
        class NtmlConfigurePopupQuizEnable
        {
            public function __construct()
            {
                add_action('pre_get_posts', array($this, 'ntml_configure_popup_quiz_enabled'));
            }
    
            public function ntml_configure_popup_quiz_enabled($query)
            {
                $user = MeprUtils::get_currentuserinfo();
                if ( (!is_admin() && ( ( !$query->is_main_query() && isset($query->query['post_type']) && ($query->query['post_type'] == 'sfwd-quiz'))) ) || ( isset($_GET['ld-lesson-page']) || isset($_POST['ld-lesson-page']) && ( !$query->is_main_query() && isset($query->query['post_type']) && ($query->query['post_type'] == 'sfwd-quiz')) ) ) {
    
                    $user = MeprUtils::get_currentuserinfo();
                    if ($user != false && isset($user->ID)) {
                        $status_of_all_products = array();
                        $all_active_products = $user->active_product_subscriptions('ids', true, true);
                        foreach ($all_active_products as $product) {
                            $quiz_status_product = get_post_meta($product, 'quiz_enabled', true);
                            $status_of_all_products[] = $quiz_status_product;
                        }
                        
                        $sub_account_status = get_user_meta($user->ID, 'mpca_corporate_account_id', true);
    
                        if( ( isset($_GET['ld-lesson-page']) || isset($_POST['ld-lesson-page']) ) ){
    
                            //error_log('mpca_corporate_account_id');
                            //error_log($sub_account_status);
                            //error_log(print_r($status_of_all_products,1));
    
                        }
    
                        if (isset($sub_account_status) && !empty($sub_account_status) && !empty($status_of_all_products) && in_array(1, $status_of_all_products)) {
                            
    
                            if( ( isset($_GET['ld-lesson-page']) || isset($_POST['ld-lesson-page']) ) ){
    
                                //error_log('status of products is true');
                            }
    
                            $category_IDs_to_show = array();
    
                            if( ( isset($_GET['ld-lesson-page']) || isset($_POST['ld-lesson-page']) ) ){
                                //error_log('enabled quiz cate');
                            }
                            
                            //get all the acf fields
                            $ld_course_mapping_with_categories = get_field('ld_course_mapping_with_categories', 'option');
    
                            if( ( isset($_GET['ld-lesson-page']) || isset($_POST['ld-lesson-page']) ) ){
                                error_log('after get field');
                            }
    
                            //user fields
                            $ld_enabled_quiz_categories = get_user_meta($user->ID, 'ld_enabled_quiz_categories', true);
                            $ld_enabled_sec_quiz_categories = get_user_meta($user->ID, 'ld_enabled_sec_quiz_categories', true);
    
                            if( ( isset($_GET['ld-lesson-page']) || isset($_POST['ld-lesson-page']) ) ){
                                error_log('enabled quiz cate');
                                error_log(print_r($ld_enabled_quiz_categories));
                            }
    
                            if (!empty($ld_enabled_quiz_categories)) {
                                
                                //for all grader
                                $all_grade_user_current_group = get_user_meta($user->ID, 'user_current_class', true);
                                
                                if (isset($all_grade_user_current_group) && !empty($all_grade_user_current_group)) {
                                    
                                    $all_courses_ids = array();
                                    $getCurrentBookAssociatedTerms = array();
                                    $course_associated_with_group = NtmlUtils::getGroupCourses($all_grade_user_current_group);
                                    foreach ($course_associated_with_group as $course) {
                                        if (is_object($course)) {
                                            $all_courses_ids[] = $course->ID;
                                        }
                                    }
                                    if (count($all_courses_ids) == 1) {
                                        $getCurrentBookAssociatedTerms = NtmlUtils::get_category_for_passed_course_id($all_courses_ids[0], $ld_course_mapping_with_categories);
                                    } else {
                                        foreach ($all_courses_ids as $courses_id) {
                                            $getCurrentBookAssociatedTerms = array_merge($getCurrentBookAssociatedTerms, NtmlUtils::get_category_for_passed_course_id($courses_id, $ld_course_mapping_with_categories));
                                        }
                                    }
    
                                    foreach ($getCurrentBookAssociatedTerms as $quiz_cat) {
                                        $term = get_term_by('id', absint($quiz_cat), 'ld_quiz_category');
                                        if (strpos($term->slug, $ld_enabled_quiz_categories)) {
                                            $category_IDs_to_show[] = $term->term_id;
                                        }
                                    }
    
                                    if (isset($ld_enabled_sec_quiz_categories) && !empty($ld_enabled_sec_quiz_categories)) {
                                        $category_IDs_to_show = array_merge($category_IDs_to_show, $ld_enabled_sec_quiz_categories);
                                    }
    
                                    $query->set('tax_query',
                                        array(
                                            array(
                                                'taxonomy' => 'ld_quiz_category',
                                                'field' => 'term_id',
                                                'terms' => $category_IDs_to_show,
                                                'operator' => 'IN'
                                            )
                                        )
                                    );
    
                                    return $query;
    
                                } else {
                                    //for single grader
                                    GFCommon::log_debug(' debug inside else ');
                                    $member_of_groups = groups_get_user_groups($user->ID);
                                    $course_associated_with_single_grader = array();
                                    $all_courses_ids_single_grade = array();
                                    $getCurrentBookAssociatedTerms = array();
    
                                    if ($member_of_groups['total'] == 0) {
                                        return $query;
                                    }
    
                                    $first_group_member_enrolled = $member_of_groups['groups'];
                                    foreach ($first_group_member_enrolled as $group) {
                                        $course_associated_with_single_grader = array_merge($course_associated_with_single_grader, NtmlUtils::getGroupCourses($group));
                                    }
                                    
                                    foreach ($course_associated_with_single_grader as $course) {
                                        if (is_object($course)) {
                                            $all_courses_ids_single_grade[] = $course->ID;
                                        }
                                    }
                                    
                                    if (count($all_courses_ids_single_grade) == 1) {
                                        $getCurrentBookAssociatedTerms = NtmlUtils::get_category_for_passed_course_id($all_courses_ids_single_grade[0], $ld_course_mapping_with_categories);
                                    } else {
                                        foreach ($all_courses_ids_single_grade as $courses_id) {
                                            $getCurrentBookAssociatedTerms = array_merge($getCurrentBookAssociatedTerms, NtmlUtils::get_category_for_passed_course_id($courses_id, $ld_course_mapping_with_categories));
                                        }
                                    }
    
                                    //get all primary
                                    foreach ($getCurrentBookAssociatedTerms as $quiz_cat) {
                                        $term = get_term_by('id', absint($quiz_cat), 'ld_quiz_category');
                                        if (strpos($term->slug, $ld_enabled_quiz_categories)) {
                                            $category_IDs_to_show[] = $term->term_id;
                                        }
                                    }
                                    
                                    //merge primary with secondary
                                    if (isset($ld_enabled_sec_quiz_categories) && !empty($ld_enabled_sec_quiz_categories)) {
                                        $category_IDs_to_show = array_merge($category_IDs_to_show, $ld_enabled_sec_quiz_categories);
                                    }
                                    
                                    //id before
                                    $query->set('tax_query',
                                        array(
                                            array(
                                                'taxonomy' => 'ld_quiz_category',
                                                'field' => 'term_id',
                                                'terms' => $category_IDs_to_show,
                                                'operator' => 'IN'
                                            )
                                        )
                                    );
                                    
                                }
                            }
                        }
    
                    }
                }
                return $query;
            }
        }
    
        new NtmlConfigurePopupQuizEnable();
    }
  • Can you guys please help?

  • Your problem is here

    
    if ( (!is_admin() .....
    

    is_admin() always returns true for AJAX requests. You need to adjust this if statement to allow it to run during ajax.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.