I'm trying to find related students (leerling) for a specific location (many-to-many relationship) of whose custom field workshop-leerling-locatie equals the name of a specific workshop.
I have this, but it doesn't return any post. I would expect one result.
I've tried the meta_key without the wpcf-, but that didn't work. If I leave out the args parameter, I get all related student-locations.
Hello. Thank you for contacting the Toolset support.
Can you please share details where you added the code and where you are trying to apply that code.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Hi Minesh, I need to test if the maximum number of students isn't exceeded for any given workshop. If it is, then the workshop won't show up and new students can't subscribe to it anymore.
Your code is still working fine, though.(I only changed the toolset_get_related_posts from legacy to the new version.)
Hi Minesh, I need to test if the maximum number of students isn't exceeded for any given workshop. If it is, then the workshop won't show up and new students can't subscribe to it anymore.
==>
What is the rule to check the maximum number of student limit, I mean with what to compare to check maximum student where maximum students number limit is defined?
If the limit is reached, do you want to remove that workshop from workshop dropdown?
// Find workshops for which the max number of students have applied.
foreach ($workshop_ids[0] as $workshop_id) {
// Get the maximum as set by the administrator in the custom field "max-leerlingen" of CPT "Workshop".
$max_students = get_post_meta($workshop_id, "wpcf-max-leerlingen", "true");
//Only check if max_students is not null or zero.
if ( $max_students != 0 or is_null($max_students) ) {
//DEBUG
print("<pre>");
// I just figured out that the workshops are stored with their ids in the intermediary post, so this is not necessary.
// $workshop_name = get_the_title($workshop_id);
// print_r("max students of $workshop_name: $max_students");
// Now get the related students of a certain location through the leerling-lokatie relationship where the value of 'workshop-leerling-locatie' equals the workshop_id that we are working with here.
$cur_students = array();
$cur_students = toolset_get_related_posts(
$locid,
'leerling-lokatie',
[ 'return' => 'post_object'
, 'query_by_role' => 'child'
, 'role_to_return' => 'parent'
, 'args' => [ 'meta_key' => 'wpcf-workshop-leerling-locatie', 'meta_value' => $workshop_id ]
] );
// Here comes the rule!
if ( count($cur_students) >= $max_students) {
//REMOVE WORKSHOP_ID FROM THE ARRAY (TODO)
}
//DEBUG
print("\nNumber of already registered students: " . count($cur_students_locs) . "</pre>");
}
}
My problem is solely with the toolset_get_related_posts function that doesn't seem to return the proper amount of students when the 'args' parameter is added. Without the statement is working correctly and gives me the expected (total) number of students.
BTW, I've tried enclosing $workshop_id in the args parameter with single quotes. Doesn't work either.