I currently have a loop to display custom posts for courses. Now I also have a custom post type for universities. Both have a certain 'schoolID', so I can link the courses to the university.
Now I want to display certain information from a university, in the loop of the courses. So for example, it displays the name of the university, at every course, within the loop.
How would one do this?
I currently have a loop to display custom posts for courses. Now I also have a custom post type for universities. Both have a certain 'schoolID', so I can link the courses to the university.
Now I want to display certain information from a university, in the loop of the courses. So for example, it displays the name of the university, at every course, within the loop.
How would one do this?
Share Improve this question asked Oct 16, 2019 at 15:59 KoopmanKoopman 111 bronze badge 5 |1 Answer
Reset to default 0With 2 separate post types, basically, you need to run two queries. Where and how depends on what the real data look like. For example, you could run query 1 to get info on all universities, and query 2 to get the specific courses you want to show. Then loop through query 2, output its information, and using the schoolID identify the university within the query 1 data and output that. The risk is if you have info on a whole lot of universities, you're querying to get info on every single one every single time.
Or, you could run query 1 to get the specific courses you want to show, if there aren't very many at a time. Loop through that query outputting the course information, and inside that loop, run query 2 to get only the specific university you need, and output its information. The downside here is, you are always running multiple queries, so if you have say 5 courses from 1 university, you are still querying to get that university's information 5 separate times.
A third and possibly more performant option: run query 1 to get the specific courses you want to show, including your schoolIDs. Then run query 2 to get only the universities with those specific schoolIDs. Then, similar to the first option, you can loop through the courses, and the trick is figuring out how to identify which university matches.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745074905a4609776.html
schoolID
, can you be more specific? Would it be possible to set the parent ID of the course to the university/school post? That would be the most performant way of doing it – Tom J Nowell ♦ Commented Oct 16, 2019 at 16:32