Support Forum
I've got an interesting use case, and I'm still wrapping my head around it.
A school wants to have its report/grade cards online, semi-automated and calculated.
The idea is, that the teacher fills the data on a form, and the system outputs the data plus some calculations (averages and such), being able to visualize it on single and multiple views (for siblings).
I'm thinking of setting a CPT per subject/course and having a post per student enrolled in it, with a Group/Repeater per month/cycle. And then on a View, display a table with all the subjects the student is enrolled in (multiple CPTs data in the same view, to display the different subjects), and use Twig to calculate the averages where needed.
What I'm not sure about is if it's possible to "filter" the posts to display only the ones that match a specific field to the logged user (WP User ID).
Does that sound feasible? Or am I complicating things too much?
Hi Jorge,
Yes, it is possible to filter posts by some parameters. You can follow this documentation to know how to use the WP Query (function get_posts()
) in View https://docs.metabox.io/extensions/mb-views/#running-php-functions
And please note that, it's really hard to query posts by subfield value (in a group). Please use the top field if you want to query posts by custom field value.
Thanks Long, yeah I moved everything to a top field.
I'm trying right now to achieve something but I feel I'm not grasping something (could it be a relationship?).
There are two CTPs: One for records/scores, the other for weighing. The idea is to do some operations between the two as follows: 'Homework score * Homework weigh = Homework final.'
However, if I do a query to create a for loop to display each entry of the Records, I do not know how to access the weighs.
On a side note, would it be better to keep this (which I feel it's a bit complex) in a View or would it be better to move it to a PHP file?
Hi Jorge,
You can create a relationship between CPT Record and Weighting, then set the relation when editing the Record post.
To query Weighting posts in the loop of Record, you can create a nested loop by following this topic https://support.metabox.io/topic/query-in-relationship-post-per-page/#post-29149
Using PHP or Twig (in View) code is fine but you need to have the basic knowledge to understand how it works.
That's great Long, thank you. I got the nested loop working properly.
The tricky part for me was setting the relationship 'from' because I wasn't sure where does that come from, but from the context of the queries made sense.
{% set weigh_args = { post_type: 'weight', nopaging: true, relationship: {id: 'weight_grades', from: grade.ID} } %}
Since I will be using fields from both loops across the entire view. I've got a question regarding best practices.
Is it better to get the arguments and "open" the For loop, then get the arguments and open the nested loop, do the content and stuff, then close both loops? Or open the main loop, and where the relationship fields are needed, open the nested loop?
Hi,
In my experience, you just need to set the argument and open a loop where you need to show list posts. Don't open a loop in the top or middle of the code where you do not need to show the posts.