0

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?

  • Have you tried using a second loop? When you say they're connected via a 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 Oct 16 '19 at 16:32
  • Hi Tom, thank you for replying. "Have you tried using a second loop?" I'm not sure how I would set this up? "When you say they're connected via a schoolID, can you be more specific?" I mean they both have a field called 'schoolID'. So if the course has 'schoolID'1 and the university as well, I can easily display all courses from that id. "Would it be possible to set the parent ID of the course to the university/school post?" Not sure what you mean by this? – Koopman Oct 16 '19 at 16:53
  • Can you be more specific by what you mean by field? E.g. is this post meta? An ACF field? A term in a taxonomy? A category? Tag? Also can you edit your question to include the code for your loop? – Tom J Nowell Oct 16 '19 at 23:57
  • Hi Tom, I'm sorry it's an ACF field indeed. – Koopman Oct 17 '19 at 4:27
  • 1
    So in short what I'm looking for: if is ID X (acf field) for the current custom post type, show value of field Y (acf field) from different custom post type. – Koopman Oct 17 '19 at 5:38
0

With 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.