Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

ACF Conditional display of a custom taxonomy based according to user (when creating/editing a post) WordPress

  • SOLVED

This may be complicated to explain but i’ll give it my best shot.

The background…

I’m making a site whereby teachers from different schools can create posts about different students from different schools. Importantly, teachers may only be able to view/choose/check/tick students from the same school as themselves.

I have created new user roles (and capabilities) for teachers according to the name of their school e.g.
XYZ primary school teachers are assigned the user role: xyz and capability: xyz
ABC highschool teachers are assigned the user role: abc and capability: abc

I have ACF Pro and CPTUI plugin to create new custom taxonomies.

I have created a taxonomy student that contains the names of all the students.

I have created a taxonomy school that contains the names of the different schools.

WIth ACF I have created a field group that assigns the school taxonomy to the student taxonomy. This means when I create or edit an individual student term the student has a school taxonomy radio button for me to choose which school the student is assigned/belongs to.

I have also done the same to the users (teachers). I have created a field group that assigns the school taxonomy to the users. This means when I create or edit a user (teacher) the user has a school taxonomy radio button for me to choose which user the user/teacher is assigned/belongs to.

As you can see the above field groups that assign the school taxonomy to the users and student taxonomy gives both the users/teachers and students a common link/taxonomy (school).

I have created another field group assigned to a post with many other fields.

This is where my question really starts….

When a user/teacher creates a post one of the fields the user must tick is the student. However, all the student names are listed. I only want the students(terms) assigned with the same school taxonomy as the user creating the post to be visible in the list of students. In other words only populate the student taxonomy with those students(terms) that have the same school(term) as the user(teacher) creating the post.

When creating or editing a post…

IF current_user (has_term(XYZ) in_tax(school))
THEN populate taxonomy(student) with (has_term(XYZ) in_tax(school))
Another way may be to have a condition whereby…

IF current_user (user_capability(xyz)
THEN populate taxonomy(student) with (has_term(XYZ) in_tax(school))
Either way is fine by me the important thing is to only display the student names that belong to the same school as the teachers creating the post.

https://docs.google.com/document/d/1Gc-ArnEM9M_Jge13jkoscZ5v_AZgNgrbK9khXZbQqcE/edit?usp=sharing

Answers (6)

2019-01-24

Hugo Gonçalves answers:

Hi pjeaje!

Need some help from you, i have a couple of doubts before i can dig any further.

---
"create 2 new user capabilities(school1,school2)"
what do you mean by this? Do these capabilities need any specific abilities?

---
"When a user/teacher creates a post one of the fields the user must tick is the student. However, all the student names are listed. I only want the students(terms) assigned with the same school taxonomy as the user creating the post to be visible in the list of students. In other words only populate the student taxonomy with those students(terms) that have the same school(term) as the user(teacher) creating the post.

When creating or editing a post…

IF current_user (has_term(XYZ) in_tax(school))
THEN populate taxonomy(student) with (has_term(XYZ) in_tax(school))
Another way may be to have a condition whereby…

IF current_user (user_capability(xyz)
THEN populate taxonomy(student) with (has_term(XYZ) in_tax(school))
"

What is the end goal you are trying to achieve? To have those students read only the posts published by a certain teacher that is associated with them?

Cheers
Hugo


pjeaje comments:

what do you mean by this? Do these capabilities need any specific abilities?
Don't worry too much about the user roles and capabilities at the moment. But I need each user/teacher to be assigned the role "teacher" and capability "teacher".

What is the end goal you are trying to achieve?
In the new post/post edit ACF box, only populate the student taxonomy with those students(terms) that have the same school(term) as the user(teacher) creating the post.

Make sure you read the Google doc.


pjeaje comments:

You can now comment on the Google doc


Hugo Gonçalves comments:

I already have read the doc.

I was asking about the end goal, because there could be an easier solution for your problem, specially on the maintenance end. It seems a bit too much, to keep adding the students each time, a teacher publishes a new post.

What i mean is, could one of these fellows be a solution for you?
https://wordpress.org/plugins/coursepress/
https://wordpress.org/plugins/wpschoolpress/
https://wordpress.org/plugins/campus-directory/

Cheers
Hugo


pjeaje comments:

That's not what I'm asking... This is an ACF field populate issue only


pjeaje comments:

I'm going to have 1000's of students (terms) and 100's of teachers (users)
when a teacher creates/edits a post that teacher should only be able to see those students in the list that have the same custom school term as themselves.


pjeaje comments:

It's simply a privacy measure to stop teachers from other schools seeing the names of students not in their school.


Hugo Gonçalves comments:

Hi pjeaje, hope i'm still on time to help you out.

Just one question i have, will the students have access to the backend?
Tip: Since you will be back and forth with multiple users for testing purposes, this mighty come handy: // https://wordpress.org/plugins/user-switching/

Two steps.

First:
On the ACF settings screen, in the Achievement field group, change the Student name field type from taxonomy to radio (we'll be getting the students with code).

Second:
In the functions.php file, add this code:

function acf_field_filtered_students($field) {
$teacher_user_meta_tax_id = get_user_meta(get_current_user_id(), 'schools'); // here we grab the school assigned in the teacher User logged in (returns the ID number of that school/taxonomy term)

// https://codex.wordpress.org/Function_Reference/get_current_screen
$current_screen = get_current_screen(); // here we check the current we are on (we don't want this running in the ACF settings page)
$where_do_you_want_it_to_show = 'page'; // or 'post' or other yuo are aiming for

// Are we in the admin/CMS? And does the current user have an assigned school? And is this a post admin page? if these are all true, then lets continue
if (is_admin() && !empty($teacher_user_meta_tax_id) && $current_screen->id === $where_do_you_want_it_to_show) {

// lets create a temp array to hold only the students we want to show
static $this_student_list = null;
$field['choices'] = array();
if($this_student_list !== null) { return $this_student_list; }
$this_student_list = array();

$student_taxonomy_terms = get_terms( array( // get those pesky students (all of them)!!
'taxonomy' => 'student',
'hide_empty' => false,
) );

if(is_array($student_taxonomy_terms) && (count($student_taxonomy_terms) > 0)) { // do we have any students here? If so proceed
foreach($student_taxonomy_terms as $term) { // iterate each student
$term_meta_school = get_term_meta($term->term_id, 'school'); // get the school that each student has assigned to (returns the ID number of that school/taxonomy term)
if ($term_meta_school[0] === $teacher_user_meta_tax_id[0][0]) { // do the IDs of both teacher User (currently logged in) and each student match?
$this_student_list[$term->term_id] = $term->name; // If so than add that student to our temp list
}
}
}
$field['choices'] = $this_student_list; // assign the temp array of corresponding students to the choices of this field
}
return $field; // et voila
}
// https://www.advancedcustomfields.com/resources/acf-load_field/
add_filter('acf/load_field/name=student_name', 'acf_field_filtered_students');


Let me know how it turned out.

Cheers
Hugo


Hugo Gonçalves comments:

Correction: since you are using this in posts not pages i'll add that as the default:

function acf_field_filtered_students($field) {
$teacher_user_meta_tax_id = get_user_meta(get_current_user_id(), 'schools'); // here we grab the school assigned to the teacher User logged in (returns the ID number of that school/taxonomy term)

// https://codex.wordpress.org/Function_Reference/get_current_screen
$current_screen = get_current_screen(); // here we check the current we are on (we don't want this running in the ACF settings page)
$where_do_you_want_it_to_show = 'post'; // or 'page' or other you are aiming for

// Are we in the admin/CMS? And does the current user have an assigned school? And is this a post admin page? if these are all true, then lets continue
if (is_admin() && !empty($teacher_user_meta_tax_id) && $current_screen->id === $where_do_you_want_it_to_show) {

// lets create a temp array to hold only the students we want to show
static $this_student_list = null;
$field['choices'] = array();
if($this_student_list !== null) { return $this_student_list; }
$this_student_list = array();

$student_taxonomy_terms = get_terms( array( // get those pesky students (all of them)!!
'taxonomy' => 'student',
'hide_empty' => false,
) );

if(is_array($student_taxonomy_terms) && (count($student_taxonomy_terms) > 0)) { // do we have any students here? If so proceed
foreach($student_taxonomy_terms as $term) { // iterate each student
$term_meta_school = get_term_meta($term->term_id, 'school'); // get the school that each student has assigned to (returns the ID number of that school/taxonomy term)
if ($term_meta_school[0] === $teacher_user_meta_tax_id[0][0]) { // do the IDs of both teacher User (currently logged in) and each student match?
$this_student_list[$term->term_id] = $term->name; // If so than add that student to our temp list
}
}
}
$field['choices'] = $this_student_list; // assign the temp array of corresponding students to the choices of this field
}
return $field; // et voila
}
// https://www.advancedcustomfields.com/resources/acf-load_field/
add_filter('acf/load_field/name=student_name', 'acf_field_filtered_students');


pjeaje comments:

Thanks i'll test it out.

2019-01-24

Fahid Javid answers:

Hi,

Please, can you explain it in a video?

Thanks


pjeaje comments:

No sorry.


pjeaje comments:

What don't you understand?


pjeaje comments:

https://docs.google.com/document/d/1Gc-ArnEM9M_Jge13jkoscZ5v_AZgNgrbK9khXZbQqcE/edit?usp=sharing

2019-01-24

Arnav Joy answers:

Yes I would also like to see a video


pjeaje comments:

No sorry


pjeaje comments:

What don't you understand?


pjeaje comments:

https://docs.google.com/document/d/1Gc-ArnEM9M_Jge13jkoscZ5v_AZgNgrbK9khXZbQqcE/edit?usp=sharing

2019-01-24

Cesar Contreras answers:

can you explain it in a video?


pjeaje comments:

https://docs.google.com/document/d/1Gc-ArnEM9M_Jge13jkoscZ5v_AZgNgrbK9khXZbQqcE/edit?usp=sharing

2019-01-24

Echeverri answers:

Yes I would also like to see a video, for understend a litle more the problem


pjeaje comments:

https://docs.google.com/document/d/1Gc-ArnEM9M_Jge13jkoscZ5v_AZgNgrbK9khXZbQqcE/edit?usp=sharing

2019-01-30

User180711 answers:

<a href=https://www.aspiremotoring.com/collections/Avid1-wheels>Avid1 wheel</a>