Home › Forums › General Issues › get_field in pre_get_posts now breaks/loops
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?
This code used to work before the update.
The code will alter the query in the posts list in edit.php to filter categories
// ********** 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);
}
Is it causing any errors? Do you have debugging on? https://codex.wordpress.org/WP_DEBUG
I don’t see anything obviously wrong with your code, so let me know if you get any errors or warnings with debug display on.
Thanks John,
within the debugger, pre_get_posts was invoked twice. I have added this enforcement if($query->is_admin && $query->query['post_type'] == "post")
{and now things are working as usual
So, why did it break when you updated?
The answer is that the pre_get_posts filter was running when ACF was getting the field. I’m assuming here that you were using ACF4. ACF4 did not use a post type for acf fields and ACF 5 does and it uses WP_Query() to get the field info.
Sorry, I should have caught that the condition was not specific enough.
Yes I was running ACF 4 before the switch. Is there an article on changes from 4 to 5?
Could you clarify exactly how did ACF4 work and how does ACF5 works?
Sorry, there used to be document with all the changes, but I can’t find it, and I don’t remember them all.
The topic ‘get_field in pre_get_posts now breaks/loops’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.