Title: Storing Variables in WordPress database
Last modified: August 3, 2018

---

# Storing Variables in WordPress database

 *  [thewildwebnl](https://wordpress.org/support/users/thewildwebnl/)
 * (@thewildwebnl)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/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 7 years, 10 months ago by [thewildwebnl](https://wordpress.org/support/users/thewildwebnl/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Anonymous User
 * (@anonymized-16282322)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/storing-variables-in-wordpress-database/#post-10554982)
 * Well, for example you could use custom post types, I do that with the third party
   data on my WP website.
 *  [Rey Den Nalasa](https://wordpress.org/support/users/raiden08/)
 * (@raiden08)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/storing-variables-in-wordpress-database/#post-10555183)
 * First you need to create a [post type](https://codex.wordpress.org/Post_Types)
   after that you can store the data in [post_meta](https://developer.wordpress.org/reference/functions/get_post_meta/).
   Now you want it to store once a day so you have to use [wp_cron](https://codex.wordpress.org/Function_Reference/wp_cron).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/storing-variables-in-wordpress-database/#post-10556957)
 * 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](https://codex.wordpress.org/Function_Reference/wp_schedule_event)
   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)

The topic ‘Storing Variables in WordPress database’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 4 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 10 months ago](https://wordpress.org/support/topic/storing-variables-in-wordpress-database/#post-10556957)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
