Posted on

Delete Expired Jobs By Locomotive

Here is a wonderful plugin Locomotive from Reaktiv Studios.

What Locomotive does: It creates batch processes in WordPress easily. If you want to query a large dataset and perform simple and repeatable actions, then Locomotive can do easily for you.

Please check here for more details

Well, today I will explain how we can easily get ride of expired jobs in WP Job Manager.

Step 1: We need to find out those expired jobs.

function find_expired_jobs() {
  register_batch_process( array(
     'name' => 'Delete Expired Jobs',
     'type' => 'post',
     'args' => array(
         'post_type' => 'job_listing',
         'post_status' => 'expired'
      'callback' => 'delete_expired_jobs', // callback function 
  ) );
add_action( 'locomotive_init', 'find_expired_jobs' );

Step 2:
Also we have added a callback function delete_expired_jobs. We need to delete all the expired jobs.

function delete_expired_jobs( $post ) {
    $media = get_children(array(
        'post_parent'   => $post->ID,
        'post_type'     => 'attachment'
    if (!empty($media)) {
        foreach ($media as $file) {
            wp_delete_attachment($file->ID); // @todo need to check if media file is not attached to other post.
    wp_delete_post( $post->ID, true );

You can find details code here.