-1

I'm building a stock database with WordPress. There is a custom Post Type stocks with the fields last_price and ytd_return (ACF). The database will hold around 2000 stocks.

For the stock quotes I plan to use alphavantage (https://www.alphavantage.co) which offers a REST API to pull updated quotes.

As I only want end-of-day data I plan the following: Every night run a "CronJob" inside Wordpress which pulls the updated stock quotes for all the stocks and calculates YTD performance.

Now the question: What is the most straightforward way to do this? Is it wise to loop through all my posts (stocks) and use wp_remote_get() to update the data? I expect to get problems with php execution times...

Or is there a better way to accomplish my goal with another php solution? For example I thought about setting up an own table in the sql db and insert the new data there.

5
  • 1
    I don't really understand your question. So you fetch data from a remote source and according to that need to update 2 custom fields for ~2000 posts? If you run a proper cron (be it php /.../../wp-cron.php or via WP CLI), execution time won't be a problem, as the process may take as long as it needs
    – kero
    May 22, 2018 at 12:06
  • OK, thank you. Maybe that really isn't a problem then.... :) Maybe my idea works and I thought too much about it. let's try.... May 22, 2018 at 12:17
  • You're welcome. Also, if the WordPress/PHP method is too slow, you could directly construct an SQL query. If none of that really works: do it in batches: Have it start at 2am, and update X posts every 5 minute (have one cron that runs daily at night, this then starts several "sub"-crons)
    – kero
    May 22, 2018 at 12:22
  • I will try that. Do you maybe have a small example code for such an cron + subcron job? May 22, 2018 at 12:31
  • I gave an answer that outlined this, but no real code there, sorry.
    – kero
    May 22, 2018 at 12:39

1 Answer 1

0

I would recommend you to separate your data from wp-data. To realize your idea, setup a new data table for time series data. Write a php script to request your data from desired source and create a cronjob. If your hoster does not provide cronjob creation, use a cronjob service. Depending on the amount of data you request, you probably have to create approx. 10 cronjobs.

Then, create a new table for YTD performance values and create a new php script to calculate YTD performance. This should work very quickly, so you will probably need only one cronjob (I suppose, maximum 4 cronjobs).

Now you will have the time series data and the performance data in two separate tables. To show YTD data in your WordPress, create a shortcode in your functions.php like I did here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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