存储Wordpress自定义数据的最佳做法是什么

时间:2021-06-15 16:57:26

Context: I need to create a form with a lot of customisations and quite a few fields (20+) for a site and rather than installing a plugin and modifying the hell out of it (or learning the plugin's API) I decided to write my own plugin. Since I'm doing this from scratch the question of storing data had to be answered.

上下文:我需要为一个站点创建一个包含大量自定义和很多字段(20+)的表单,而不是安装插件并修改它的地狱(或者学习插件的API)我决定写我的自己的插件。因为我从头开始这样做,所以必须回答存储数据的问题。

Most people will suggest using a custom post type and meta data rather than a custom table and while I'm familiar with custom post types and taxonomies etc I wonder whether this is best for pure data storage that amounts to a log.

大多数人会建议使用自定义的帖子类型和元数据而不是自定义表格,虽然我熟悉自定义帖子类型和分类法等,但我想知道这是否适用于相当于日志的纯数据存储。

The form is single use and the data I'm storing won't be accessible on the front end of the site and will only be "viewed" on WP-admin and that would most likely just be a single page with a filterable table.

该表单是一次性使用,我存储的数据将无法在网站的前端访问,只能在WP-admin上“查看”,而且很可能只是一个带有可过滤表的单页。

So the question is does this warrant using a custom post type and meta-data or should I just create a table and use the $wpdb class since that simplifies CRUD and still sticks to the "Wordpress way" of doing things.

所以问题是这是保证使用自定义的帖子类型和元数据还是我应该创建一个表并使用$ wpdb类,因为这简化了CRUD并且仍然坚持使用“Wordpress方式”做事。

Reading: This post makes a good case for a custom table, especially since their example is about storing private data, questions of efficiency and privacy arose. This question, answered a few years ago has the opposite opinion but the use-case was different since the data being stored was for public display.

阅读:这篇文章为自定义表提供了一个很好的案例,特别是因为他们的例子是关于存储私人数据,效率和隐私问题出现了。几年前回答的这个问题有相反的意见,但用例不同,因为存储的数据是公开展示的。

3 个解决方案

#1


4  

I would highly suggest using a separate table. The WP table postmeta is usually filled with a lot of info from a lot of different plugins, and quite often ends up being the biggest or the biggest table in the database.

Apart from that, if saved in the postmeta table it will always be also partially saved in the posts table, since these two need each other for the information to be connected and complete. So if you were exporting/importing to another database, you would have to get involved in a very unpleasant process where the custom posts need to be the same ID as they were in the last database

Furthermore, the data is very easily accessible if in a seperate table, and should be easy to read even from phpmyadmin and it should be quite easy to code a filterable table using the $wpdb class if you have just basic sql knowledge.

All this comes from my recent experience with merging 2 big wordpress websites into one, and having a lot of info saved as postmeta... I really wish most of it were saved in a custom table, as it would make my life much easier.

The only reason for using meta and a custom post type would be that it is faster and easier(at least in my experience). Hope this helps, I'm really interested to see if there are other opinions. Good luck with your project!

我强烈建议使用单独的表格。 WP表postmeta通常充满来自许多不同插件的大量信息,并且通常最终成为数据库中最大或最大的表。除此之外,如果保存在postmeta表中,它将始终部分保存在posts表中,因为这两者需要彼此连接和完成信息。因此,如果您要导出/导入到另一个数据库,则必须参与一个非常不愉快的过程,其中自定义帖子需要与上一个数据库中的ID相同。此外,如果在数据库中,数据非常容易访问单独的表,即使从phpmyadmin应该很容易阅读,如果你只有基本的sql知识,使用$ wpdb类编写可过滤的表应该很容易。所有这些都来自于我最近将两个大wordpress网站合并为一个的经验,并将大量信息保存为postmeta ...我真的希望大部分信息保存在自定义表格中,因为它会让我的生活更轻松。使用元和自定义帖子类型的唯一原因是它更快更容易(至少根据我的经验)。希望这有帮助,我真的很想知道是否还有其他意见。祝你的项目好运!

#2


2  

Interesting question!

I normally use (as you mentioned) custom post types and taxonomies for such cases. So that would be my way to go, the only thing that I'm bit scared of is where you mentioned "..amounts to a log.."

我通常使用(如您所述)自定义帖子类型和分类法来处理此类情况。这就是我要走的路,我唯一害怕的就是你提到“..对日志的数量......”

I'm pretty sure you would be better off using a separate table, it just sucks to work through the postmeta table. At the end if you know how to use $wpdb class you should be as fine!

我很确定你最好使用一个单独的表,只是很难通过postmeta表。最后,如果你知道如何使用$ wpdb类,你应该没问题!

Looking forward to others input

期待别人的投入

#3


1  

In most cases it is easy and cool to use a custom post type with meta-data, because it is fast done and you can use all the cool wordpress functions for it.

在大多数情况下,使用带有元数据的自定义帖子类型很容易和很酷,因为它快速完成,您可以使用所有酷的wordpress功能。

A case where it is better to use a new table in the database is when you have to save a lot of data, i.e. access log or similar.

最好在数据库中使用新表的情况是需要保存大量数据,即访问日志或类似数据。

WooCommerce (a plugin from the wordpress people) save the products as CPT and all data that belongs as meta-data.

WooCommerce(来自wordpress人的插件)将产品保存为CPT和属于元数据的所有数据。

#1


4  

I would highly suggest using a separate table. The WP table postmeta is usually filled with a lot of info from a lot of different plugins, and quite often ends up being the biggest or the biggest table in the database.

Apart from that, if saved in the postmeta table it will always be also partially saved in the posts table, since these two need each other for the information to be connected and complete. So if you were exporting/importing to another database, you would have to get involved in a very unpleasant process where the custom posts need to be the same ID as they were in the last database

Furthermore, the data is very easily accessible if in a seperate table, and should be easy to read even from phpmyadmin and it should be quite easy to code a filterable table using the $wpdb class if you have just basic sql knowledge.

All this comes from my recent experience with merging 2 big wordpress websites into one, and having a lot of info saved as postmeta... I really wish most of it were saved in a custom table, as it would make my life much easier.

The only reason for using meta and a custom post type would be that it is faster and easier(at least in my experience). Hope this helps, I'm really interested to see if there are other opinions. Good luck with your project!

我强烈建议使用单独的表格。 WP表postmeta通常充满来自许多不同插件的大量信息,并且通常最终成为数据库中最大或最大的表。除此之外,如果保存在postmeta表中,它将始终部分保存在posts表中,因为这两者需要彼此连接和完成信息。因此,如果您要导出/导入到另一个数据库,则必须参与一个非常不愉快的过程,其中自定义帖子需要与上一个数据库中的ID相同。此外,如果在数据库中,数据非常容易访问单独的表,即使从phpmyadmin应该很容易阅读,如果你只有基本的sql知识,使用$ wpdb类编写可过滤的表应该很容易。所有这些都来自于我最近将两个大wordpress网站合并为一个的经验,并将大量信息保存为postmeta ...我真的希望大部分信息保存在自定义表格中,因为它会让我的生活更轻松。使用元和自定义帖子类型的唯一原因是它更快更容易(至少根据我的经验)。希望这有帮助,我真的很想知道是否还有其他意见。祝你的项目好运!

#2


2  

Interesting question!

I normally use (as you mentioned) custom post types and taxonomies for such cases. So that would be my way to go, the only thing that I'm bit scared of is where you mentioned "..amounts to a log.."

我通常使用(如您所述)自定义帖子类型和分类法来处理此类情况。这就是我要走的路,我唯一害怕的就是你提到“..对日志的数量......”

I'm pretty sure you would be better off using a separate table, it just sucks to work through the postmeta table. At the end if you know how to use $wpdb class you should be as fine!

我很确定你最好使用一个单独的表,只是很难通过postmeta表。最后,如果你知道如何使用$ wpdb类,你应该没问题!

Looking forward to others input

期待别人的投入

#3


1  

In most cases it is easy and cool to use a custom post type with meta-data, because it is fast done and you can use all the cool wordpress functions for it.

在大多数情况下,使用带有元数据的自定义帖子类型很容易和很酷,因为它快速完成,您可以使用所有酷的wordpress功能。

A case where it is better to use a new table in the database is when you have to save a lot of data, i.e. access log or similar.

最好在数据库中使用新表的情况是需要保存大量数据,即访问日志或类似数据。

WooCommerce (a plugin from the wordpress people) save the products as CPT and all data that belongs as meta-data.

WooCommerce(来自wordpress人的插件)将产品保存为CPT和属于元数据的所有数据。