• I am creating a plugin for myself which I’ll be using in conjunction with two already published in WP repository.

    I am using WP Web Scrapper (https://wordpress.org/plugins/wp-web-scrapper/) to scrape some data from other websites. Now this plugin saves the WP transients API and saves the data for a certain amount of time. However, I want to save the data in a new database table so that I can access it later or manipulate it to create charts and stuff.

    Now I want to run the Web Scrapper plugin once or twice every day so it fetches the data and saves it in the database. I dont know much about creating WP plugins but from what I know, you use hooks like wp_head to call your plugin function on every page (let’s say for a plugin which adds a facebook button on your posts).

    Since I do not need to call this plugin on every post, I only need to run it once every day? Which hook can I use?

    Secondly, when the plugin function is running on every post, you can get the post ID by using the get_the_id() function or WP_post class. How can I get the post ID when the plugin isn’t running on every page? I need the post ID in plugin to retrieve some post meta data which is needed to scrape the data for each post. I am putting this meta data in each using the CMB2 plugin (https://wordpress.org/plugins/cmb2/).

    Thanks.

Viewing 1 replies (of 1 total)
  • There is a cron function, details here:
    https://codex.wordpress.org/Function_Reference/wp_cron
    Be aware that it is not as precise a service as you may expect, I think its resolution is related to the frequency of page loads.

    Not so sure about your second issue, maybe you have to put some code in your child-themes header function, or a page template. Do an SQL query on your custom table, selecting rows with timestamp after NOW – 12 hours, if it succeeds then there is nothing to do, if it does not then log the ID and add a new row.

Viewing 1 replies (of 1 total)
  • The topic ‘Get post_id in a function which runs once a day?’ is closed to new replies.