database - Updating custom fields with external data

admin2025-01-08  4

For our articles on our WordPress site, we would like to display article analytics that are collected weekly for each manuscript. We have these native custom fields for each post:

  • Article Views
  • PDF Downloads
  • PubMed Central Views
  • Article Citations

That data (which is coming from different sources) is being collected for each of our articles weekly into a CSV file. I'd like to run some sort of script each week that will take that data and update the native fields in the Wordpress database (in each post) with the data coming from that CSV file.

Is there a means of writing a script that can do such a thing?

For our articles on our WordPress site, we would like to display article analytics that are collected weekly for each manuscript. We have these native custom fields for each post:

  • Article Views
  • PDF Downloads
  • PubMed Central Views
  • Article Citations

That data (which is coming from different sources) is being collected for each of our articles weekly into a CSV file. I'd like to run some sort of script each week that will take that data and update the native fields in the Wordpress database (in each post) with the data coming from that CSV file.

Is there a means of writing a script that can do such a thing?

Share Improve this question edited Jan 17, 2021 at 11:10 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Dec 12, 2014 at 13:33 Hal AtkinsHal Atkins 951 silver badge9 bronze badges 4
  • My question is about a Wodpress site - and I've attempted to indicate that more clearly in my question. Please advise if it is still "off-topic". – Hal Atkins Commented Dec 12, 2014 at 14:17
  • It was likely closed as off topic due to 3rd party plugin. If you edit out reference to ACF and make it about native custom fields, then it will be on topic. – Milo Commented Dec 12, 2014 at 14:30
  • Ah...that makes sense. Thanks for the guidance (edits made). – Hal Atkins Commented Dec 12, 2014 at 14:32
  • Take a look at how the Importer plugin works. By now we have a job description, but no really answerable question (without writing a fully fledged plugin with only a rough description). Please update your question with what you have tried and where you actually failed. – kaiser Commented Dec 12, 2014 at 15:03
Add a comment  | 

1 Answer 1

Reset to default 0

You can use PHP's fgetcsv() or str_getcsv() function to parse the uploaded CSV file. Your CSV will need to include a column with the Post's ID as well as the columns matching up to your custom field names. Loop through the CSV and for each line update the custom fields for that post using update_post_meta()

http://php.net/manual/en/function.fgetcsv.php

http://php.net/manual/en/function.str-getcsv.php

http://codex.wordpress.org/Function_Reference/update_post_meta

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269121a1328.html

最新回复(0)