Skip to content

Instantly share code, notes, and snippets.

@VR51
Created February 6, 2015 01:22
Show Gist options
  • Save VR51/396ed3be1a08b891cf8b to your computer and use it in GitHub Desktop.
Save VR51/396ed3be1a08b891cf8b to your computer and use it in GitHub Desktop.
Refreshes the date of WP Job Board job posts. Any job post older than 2 months will be automatically republished with the date current at the time the script runs.
<?php
/**
* Refresh WP Job Board job publish dates daily
* VR51.com & JournalXtra.com
* Add to your child theme's functions.php
**/
if (class_exists('Wpjb_Model_Job')) {
/* Add daily cron job to run the vr_daily_job_refresh hook */
add_action( 'wp', 'vr_job_refresh_schedule' );
function vr_job_refresh_schedule() {
if ( ! wp_next_scheduled( 'vr_daily_job_refresh' ) ) {
wp_schedule_event( time(), 'daily', 'vr_daily_job_refresh');
}
/* Clear the above if statement and uncoment next line to clear the cron job */
/* wp_clear_scheduled_hook( 'vr_daily_job_refresh' ); */
}
add_action( 'vr_daily_job_refresh', 'vr_update_job' );
function vr_update_job(){
/* Uncomment echos to see test output */
/* Count Job Posts */
$name = new Wpjb_Model_Job();
$name = $name->tableName();
global $wpdb;
$Count = $wpdb->get_var( "SELECT COUNT(*) FROM {$name}" );
/* Calculate dates references */
// echo "Server Date: ".date('Y-m-d')."\n";
$today = floor(strtotime(date('Y-m-d')))/86400;
// echo "Today's Unix Timestamp: ".$today."\n";
/* Update Job */
$n=1;
// We add 20 to the post count to account for upto 20 deleted job postings
while($n<=($Count+20)) {
$job = new Wpjb_Model_Job($n);
// echo "Count: ".$Count."\n";
$job_date = $job->job_created_at;
// echo "Original Post Date: ".$job_date."\n";
$unix_job_date = floor(strtotime($job_date))/86400;
// echo "Unix Post Date: ".$unix_job_date."\n";
if ($unix_job_date<=$today-30) {
$job->job_created_at = date('Y-m-d');
// echo "New Job Date: ".$job->job_created_at."\n";
$job->job_modified_at = date('Y-m-d');
// echo "New Modified Date: ".$job->job_modified_at."\n";
$job->save();
}
// echo $n."\n";
$n++;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment