0

I know this question was already answered. But I hope someone can help me in my specific Problem.

I have a custom Post Type. For this Post Type the user can declare 1 to X Datetime ranges with a value which is valid for this daterange.

At this time I store these Ranges in one serialized database field.

But now I need to do a sql - query to get all Posts which have valid dates in a given daterange. So e.g. "Select all Posts where the valid Daterange is between 07.12.2015 and 01.01.2016"

At this Time i only use the given wordpress tables to store the data. (Because I often read, that create own tables is not the best practice in wordpress) But now I think it is the best way to create a "post date position"-Table with the fields "post id", "position id", "from date", "to date", "value" to store these informations and can query them easily.

My question now is, is there any better way to solve this problem or is creating a own table the best practice?

Thanks

1 Answer 1

1

Use the relational database for its intended purpose - create the separate table. The rich set of time/date functionality available in your SQL queries for this example, is well worth it.

Sticking searchable/reportable data into a serialized column in the database should be the last resort not the first ;-)

2
  • :-D thanks for your answer. I think there is no absoulte right answer for my question. But imho it is the best solution to create the table. I try to only create a own table when it is needed. And in this case, it is needed. Is it a good idea to save the data in the new table and in a serialized column to avoid any data Loss when wordpress or mysql is change? Or should I following my rules and don't create data redundancy? Dec 8, 2015 at 7:36
  • WordPress will not wipe out your table when an upgrade occurs, nor would mysql. For you to lose your table you'd need to have someone purposefully go into the database and execute a DROP command on your table. Hopefully, whoever is running the installation is not going to do that, and they are also taking periodic backups of the database.
    – C C
    Dec 8, 2015 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.