admin管理员组文章数量:1323715
I am trying to develop a plugin to manage and display lists, similar to a to-do-list. Different users have access to different lists and items. I started by making a custom post type for the list items. And now I am working on a custom post type representing the lists.
I was thinking about doing something like described here, where I have a dropdown in the "item" CPT and you can select a list/lists for it. The items have a postmeta field with the id(s) of the list(s). My guess would be have a separate database table that deals with the relationship of the lists and items.
Here is the part where I struggle: A list has obviously multiple items, but any item can also be on multiple lists. How can I realize that I easily get all the items for a list and also all the lists for each item? With the method described above, it's easy to get all the lists for each item by just looking up the postmeta field with the lists for that item. But the other way around seems more complicated since the postmeta field contains a serialized array.
I have an idea how I would solve this problem with a "normal" relational database, but I cannot figure out what would be a good approach to do this with WordPress.
Is the approach with the two CPTs not suited to start with?
My guess would be to create a separate additional database table that handles the relationship between the lists and items.
I am trying to develop a plugin to manage and display lists, similar to a to-do-list. Different users have access to different lists and items. I started by making a custom post type for the list items. And now I am working on a custom post type representing the lists.
I was thinking about doing something like described here, where I have a dropdown in the "item" CPT and you can select a list/lists for it. The items have a postmeta field with the id(s) of the list(s). My guess would be have a separate database table that deals with the relationship of the lists and items.
Here is the part where I struggle: A list has obviously multiple items, but any item can also be on multiple lists. How can I realize that I easily get all the items for a list and also all the lists for each item? With the method described above, it's easy to get all the lists for each item by just looking up the postmeta field with the lists for that item. But the other way around seems more complicated since the postmeta field contains a serialized array.
I have an idea how I would solve this problem with a "normal" relational database, but I cannot figure out what would be a good approach to do this with WordPress.
Is the approach with the two CPTs not suited to start with?
My guess would be to create a separate additional database table that handles the relationship between the lists and items.
1 Answer
Reset to default 1Here are few ideas how to relate content together in WordPress. Without knowing (and understanding) all the details and requirements it's difficult to give a complete answer, but these hopefully help you explore different possible solutions.
- Store relation data in CPT post meta. You can then use
WP_Query
with meta parameters to find matching posts - Manage relations with
post_parent
property ofWP_Post
(it has its own column in DB {prefix}_posts table). Nothing stops you from relating different posts types together like this, if you handle the data saving yourself withwp_update_post()
- Create a custom taxonomy to serve as lists and use CPT as items
- Use CPT as list and store list items as serialized array / json in the post's
post_content
property - Skip CPTs altogether, store everything in custom DB tables and manage data with
$wpdb
P.s. please note that these kind of open questions usually don't have a single definite answer, but recieve many opinion-based answers each with many ifs, buts and maybes, and your question might get closed.
本文标签: plugin developmentHow do you make a list and list item relation if custom post types
版权声明:本文标题:plugin development - How do you make a list and list item relation if custom post types? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126040a2421945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论