Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best practise for storing custom data for Wordpress

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.

like image 472
Daniel Avatar asked Feb 17 '17 12:02

Daniel


2 Answers

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!

like image 192
L L Avatar answered Oct 28 '22 23:10

L L


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!

Looking forward to others input

like image 22
funkysoul Avatar answered Oct 29 '22 00:10

funkysoul