If I want to save settings for my plugin then it is pretty easy and straight forward.
Now I would like to save to the database a bit more.
A file name, and 3 other values that only apply to that file. And there are many files with those values. Is it possible to save kind of subarrays using built in database methods? How can I delete and sort them etc?
I rarely disagree with otherwise knowledgeable users, but in this case I can’t help it. In my opinion calling the usage of non-core database tables bad practice per se is just simply wrong.
The choice of whether to go with core tables or add your own depends on several factors.
A query’s execution time depends on the size of the table. Hence, if you’re planning on storing significant amounts of data, a separate table catering to just this one type of specific data set will inevitably be the more efficient solution.
If you store a lot of regular posts or CPTs alongside these specific data sets,
wp_posts
as well aswp_postmeta
can grow quickly.For me, this choice ultimately depends on how “posty” the data is. Should it support an author, comments, revisions, excerpts or the like? If so, I’ll go with CPTs and/or core functionality. If not, I’ll go with separate tables for the sake of resource usage and efficiency.
If Eugene’s notion were correct, none of the existing well-written plugins would add their own tables, which fortunately is not the case.
Using WP core DB tables is best practice
$wpdb
class.wp_options
, and by forcing the Plugin developer to consider carefully the type of data being created/stored – is it a CPT? is it a taxonomy? is it post meta?WordPress provides a means for Plugins to add tables to its database
However, for those use cases where a separate DB table is needed, be sure to use the method that WordPress provides for adding your custom table to the WordPress database, especially so that you can take advantage of the powerful
$wpdb
class. Note the information/caveats this Codex entry lists:Thus, we can conclude the following:
Non core database tables are a must if your data is more complex than WordPress post model, it is going to be huge, and it has a lot of meta details which will be searched.
EAV format WordPress uses for its post meta does not lend itself well to multi-criterion search.
If you divide your meta into many entries, you will have numerous entries per post in the post meta table, and searching any post through metas will be much slower.
If you store all metas serialized in an array and have it as only one entry in post meta, this time you will be forced to do only text searches inside that meta, and you wont be able to use comparison operators directly in your sql query.
Not a big problem if your plugin is not going to have thousands of entries and associated meta.
But a major problem if your plugin is going to do anything large.
Your situation, a file name as independent entry and 3 meta data entries attached to that entry does not seem so big. You may use wordpress post table and meta table for it.
BUT, if people are going to search for these 3 metas a lot, ESPECIALLY in conjunction, then i’d recommend that you set up separate tables.
With that format just one table with just one entry, which also contains all the metas would be ok, and would query lightning fast.
Incidentally, if you use WordPress tables and also you are using query caching, the user searches for your data would get cached over time and incur less load. But that wouldnt be so prudent as doing separate tables.
You can upload your files into media library. Each item in media library is stored it
wp_posts
table. It means that each file can have meta data. You can save as much information as you need per each file inwp_postmeta
table by using Metadata API.Yes, it is bad practice to create own table, if you can use core functionality instead.