Support » Developing with WordPress » Storing Variables in WordPress database

  • Hi,

    I’m sure this question has been answered before, but I can’t seem to find the answer. So I’m hoping someone would point me in the right direction.

    A website I’m developing has reviews with a third party. This third party provides me with an XML feed. With simplexml_load_string() I succeeded in getting the feed and putting the necessary values in variables. The variables are, grade, number of reviews and the url of the page that shows the reviews.

    Right now this happens in the header on page load and this works flaky at best.

    I would like to build a plugin that pulls the feed once a day and stores the information in the database. But where can I store these variables, I don’t think I need to make a custom database.

    How would you tackle this? In broad lines.

    • This topic was modified 2 months, 2 weeks ago by  thewildwebnl.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Well, for example you could use custom post types, I do that with the third party data on my WP website.

    First you need to create a post type after that you can store the data in post_meta. Now you want it to store once a day so you have to use wp_cron.

    Moderator bcworkz


    Good suggestions above, but the best answer depends on how you will use the data, how you are currently using posts, and how older data should be managed. For example, if your site is composed of only pages and blog posts are not used, you don’t really need a custom post type. If only the latest data is of interest, post meta may not be the best place. Storing data in options and overwriting every time fresh data comes in may be better. But if you need to keep records of previous data and you are using blog posts, the custom post type and post meta probably is your best choice.

    But if you need to make complex queries using various data elements and the records will grow to be very extensive, meta data queries are not very efficient. It could make sense to store data in a custom table (in same DB) designed for the sort of queries you will be making.

    I’m partly answering for the benefit of others that land here on search and whose data isn’t exactly like yours. From what you’ve described, I think organizing the data into a PHP array and storing it in a single option will work fine. Overwrite the data with the latest as it comes in. It doesn’t sound like you are collecting actual reviews, so there is no need to maintain a historic record.

    Using WP Cron is a great suggestion for automatically getting the data and updating the option data. But Rey’s link is a little misleading. You don’t need to call wp_cron(). Rather, schedule a daily task and let WP handle executing wp_cron(). It’s important to note that the scheduling function only needs to be executed once. Don’t put it with other code that executes on every request. Using an activation hook as in the example is one way, but anything that ensures it is called once and not on every request will be fine.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.