Support

Account

Home Forums Reply To:

  • I have recently updated ACF plugin after months. Now, a get_field in pre_get_posts will crash in a loop. Offending code: $variable = get_field(‘class’, $usrid);
    Same command for same user, run in other parts of the code, will work. Could it be that the logic has changed and ACF is not initialised when pre_get_posts is being run?

    // ********** post & media lists filtered by user role & category ****************
    add_filter(‘pre_get_posts’, ‘posts_for_current_role’);
    function posts_for_current_role($query) {
    if($query->is_admin) {
    global $students_allowed_cat;
    global $teachers_allowed_cat;
    global $pagenow;
    global $acorn_user;

    if (‘edit.php’ == $pagenow) {
    //var_dump($query);die();
    if ($query->get(‘cat’) == ‘0’ || IsNullOrEmptyString($query->get(‘cat’)) || !count($_GET)) {
    if (in_array(‘teacher’, (array)$acorn_user->roles)) {
    // Allowed all at the moment $filtered_cat = $teachers_allowed_cat;
    //$query->set(‘cat’, $filtered_cat);
    }

    if (in_array(‘student’, (array)$acorn_user->roles)) {
    //The user has the “student” role
    $usrid = “user_” . $acorn_user->ID;
    $variable = get_field(‘class’, $usrid);

    switch ($variable[0]) {
    case ‘Year 7’:
    $studcat = array(157, 158);
    break;
    case ‘Year 8’:
    $studcat = array(402, 403);
    break;
    case ‘Year 9’:
    $studcat = array(150, 152);
    break;
    }

    $query->set(‘category__in’, $studcat); //$students_allowed_cat
    }

    }
    if (empty($_GET[‘author’])) {
    if (in_array(‘student’, (array)$acorn_user->roles)) {
    $members = get_users(‘role=student&fields=id’);
    //var_dump($members); die();
    if (empty($members)) {
    $members = array(0);
    }
    $query->set(‘author__in’, $members);
    } else {
    $query->set(‘author’, ‘-1’);
    }
    }

    }

    }
    return ($query);

    }