最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Loop with custom posts, to include information from different custom post type

programmeradmin0浏览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?

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
  • 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 Commented Oct 16, 2019 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 Commented Oct 16, 2019 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 Commented Oct 16, 2019 at 23:57
  • Hi Tom, I'm sorry it's an ACF field indeed. – Koopman Commented Oct 17, 2019 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 Commented Oct 17, 2019 at 5:38
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论