The problem is that you’re trying to do your work in a stand alone PHP, this will always be fragile, and it poses a security risk!
Use a Cron Job Instead
This will trigger your code to run hourly:
add_action( 'init', function() {
// If the cron job hasn't been scheduled
if ( ! wp_next_scheduled ( 'binyomins_cron_job' )) {
// schedule it to fire an event every hour
wp_schedule_event( time(), 'hourly', 'binyomins_cron_job' );
}
// Run the video update function every time the cron event happens.
add_action( 'binyomins_cron_job', 'binyomin_video_update' );
});
function binyomin_video_update(): void {
// .... your code goes here
// The Query
$args = array(
'post_type' => 'post',
"posts_per_page" => 1,
....... etc
}
You can change it to run daily, or even register your own schedule.
You could also swap out the scheduled cron with a single event if you chose, running it via cron tools such as WP CLI or the WP Crontrol plugin.
Where Does That Code Go?
Anywhere hooks and filters usually go, a plugin, a functions.php
, etc