什么是为Wordpress存储自定义数据的最佳实践 [英] What is best practise for storing custom data for Wordpress

查看:108
本文介绍了什么是为Wordpress存储自定义数据的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我需要为网站创建具有很多自定义内容和相当多字段(超过20个)的表单,而不是安装插件并对其进行修改(或学习插件的API),我决定编写自己的插件.由于我是从头开始执行此操作,因此必须回答存储数据的问题.

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

该表单是一次性使用的,我存储的数据将无法在网站的前端访问,并且只能在WP-admin上查看",并且很可能只有一个页面,其中包含一个可过滤表格.

所以问题是,这是否需要使用自定义帖子类型和元数据,还是我应该只创建一个表并使用$ wpdb类,因为这简化了CRUD,并且仍然坚持"Wordpress方式". /p>

阅读:这个问题用例有所不同,因为要存储的数据是公开显示的.

解决方案

我强烈建议您使用单独的表. WP表postmeta通常包含来自许多不同表的大量信息.插件,并且常常最终成为数据库中最大或最大的表.

除此之外,如果将其保存在postmeta表中,则由于它们两者相互连接,信息也需要完整地保存,因此也始终会部分保存在posts表中.因此,如果要导出/导入到另一个数据库,则必须参与一个非常不愉快的过程,其中自定义帖子必须与上一个数据库中的ID相同.

此外,如果位于单独的表中,则数据非常容易访问,并且即使从phpmyadmin中也应易于读取,并且如果您只具备基本的SQL知识,则使用$ wpdb类编写可过滤表应该非常容易.

所有这些都来自我最近的经验,我将2个大型wordpress网站合并为一个,并将大量信息保存为postmeta ...我真的希望大部分信息都保存在自定义表格中,因为这将使我的生活更美好简单得多.

使用meta和自定义帖子类型的唯一原因是它更快,更轻松(至少以我的经验).希望这会有所帮助,我真的很想看看是否还有其他意见.祝您的项目好运!

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.

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.

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.

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.

解决方案

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!

这篇关于什么是为Wordpress存储自定义数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆