Halloween Sale 15% off Ends in See the plans
PHP

Automatically Expire Listings After Their Closing Date

Adds an "Expired" status and an hourly check that moves job listings (or any post type) out of public view once their closing date has passed.

Don’t have WPIDE yet? Install it on your site

Code

// Post type and the meta key holding the closing date (Y-m-d or Y-m-d\TH:i).
$wpide_expire = array(
    'post_type' => 'job',
    'meta_key'  => '_job_valid_through',
);

add_action( 'init', function () use ( $wpide_expire ) {
    register_post_status( 'expired', array(
        'label'                     => 'Expired',
        'public'                    => false,
        'protected'                 => true,
        'exclude_from_search'       => true,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        /* translators: %s: number of items */
        'label_count'               => _n_noop( 'Expired <span class="count">(%s)</span>', 'Expired <span class="count">(%s)</span>' ),
    ) );

    if ( ! wp_next_scheduled( 'wpide_expire_listings' ) ) {
        wp_schedule_event( time() + 300, 'hourly', 'wpide_expire_listings' );
    }
} );

add_action( 'wpide_expire_listings', function () use ( $wpide_expire ) {
    $ids = get_posts( array(
        'post_type'      => $wpide_expire['post_type'],
        'post_status'    => 'publish',
        'posts_per_page' => 100,
        'fields'         => 'ids',
        'no_found_rows'  => true,
        'meta_query'     => array(
            array( 'key' => $wpide_expire['meta_key'], 'value' => '', 'compare' => '!=' ),
            array( 'key' => $wpide_expire['meta_key'], 'value' => wp_date( 'Y-m-d' ), 'compare' => '<' ),
        ),
    ) );

    foreach ( $ids as $id ) {
        wp_update_post( array( 'ID' => $id, 'post_status' => 'expired' ) );
    }
} );

add_filter( 'display_post_states', function ( $states, $post ) {
    if ( 'expired' === $post->post_status ) {
        $states['expired'] = 'Expired';
    }

    return $states;
}, 10, 2 );

Notes

Defaults to jobs and the _job_valid_through date from the Job Details snippet; change the post type and meta key for events, offers or adverts. Expired items stay in the admin under "Expired".

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.