Job Expiration

Supported services: Broadbean, LogicMelon and Idibu

When your multi-posting service sends a job over to your website, they send with this an expiration date. This is sent in the date format of dd-mm-yyyy.

This expiration date will be saved in your WordPress site based on the field mappings you have set up. This is most likely to be setup to save as meta data against the newly created job post.

Even though the expiration date has been saved, it is up to your WordPress site to honour that expiration date, and expire the jobs on, or after that date.

How do I expire jobs on the expiration date?

This is a developer task and is usually achieved by creating a cron job. Essentially this is a function which runs at a specific time interval – whatever is appropriate for your site.

In WordPress cron jobs are registered, which a specific time interval and then you attach a function to the cron job to run when the job runs.

Here is an example that would check for expired jobs every 24 hours and remove jobs that are older than today.

/**
 * Registered our scheduler hook for expiring jobs.
 */
function hd_register_schedule() {

	// add our scheduled event.
	wp_schedule_event(
		time(), // when to start this event.
		'daily', // interval to run the schedule at.
		'hd_remove_expired_jobs' // name of the hook that fires when this event is fired.
	);

}

register_activation_hook( __FILE__, 'hd_register_schedule' );

/**
 * Removes the scheduled event when the plugin is deactivated.
 */
function hd_deregister_schedule() {

	// clear our scheduled event from wp
	wp_clear_scheduled_hook( 'hd_remove_expired_jobs' );

}

register_deactivation_hook( __FILE__, 'hd_deregister_schedule' );

/**
 * Removes jobs that have passed their expiry date.
 */
function hd_check_for_expired_jobs() {

	$job_ids = get_posts(
		[
			'post_type'      => 'job_listing', // change for the name of the jobs post type.
			'post_status'    => 'publish',
			'fields'         => 'ids',
			'posts_per_page' => 250,
			'meta_query'     => [
				'relation' => 'AND',
				[
					'key'     => '_job_expires', // change for the meta key storing the expiry date.
					'value'   => 0,
					'compare' => '>',
				],
				[
					'key'     => '_job_expires', // change for the meta key storing the expiry date
					'value'   => current_datetime(),
					'compare' => '<',
				],
			],
		]
	);

	if ( $job_ids ) {
		foreach ( $job_ids as $job_id ) {
			wp_trash_post( $job_id );
		}
	}

}

add_action( 'hd_remove_expired_jobs', 'hd_check_for_expired_jobs' );

This code needs to be added to your plugins root file.