Halloween Sale 15% off Ends in See the plans
PHP

"Needs Review" View for Outdated Posts

Adds a "Needs review" link above the Posts list that shows published posts not updated for a year or more, so editors can keep old content accurate.

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

Code

// Posts not modified for this many months need a review.
$wpide_review_months = 12;

add_filter( 'views_edit-post', function ( $views ) use ( $wpide_review_months ) {
    $count = get_transient( 'wpide_needs_review_count' );
    if ( false === $count ) {
        $query = new WP_Query( array(
            'post_type'      => 'post',
            'post_status'    => 'publish',
            'posts_per_page' => 1,
            'fields'         => 'ids',
            'date_query'     => array( array( 'column' => 'post_modified_gmt', 'before' => $wpide_review_months . ' months ago' ) ),
        ) );
        $count = (int) $query->found_posts;
        set_transient( 'wpide_needs_review_count', $count, HOUR_IN_SECONDS );
    }

    $current = ! empty( $_GET['wpide_review'] );
    $views['wpide_review'] = sprintf(
        '<a href="%s"%s>Needs review <span class="count">(%d)</span></a>',
        esc_url( admin_url( 'edit.php?post_status=publish&wpide_review=1&orderby=modified&order=asc' ) ),
        $current ? ' class="current" aria-current="page"' : '',
        (int) $count
    );
    return $views;
} );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_review_months ) {
    if ( ! is_admin() || ! $query->is_main_query() || empty( $_GET['wpide_review'] ) ) {
        return;
    }
    $query->set( 'date_query', array( array( 'column' => 'post_modified_gmt', 'before' => $wpide_review_months . ' months ago' ) ) );
} );

Notes

Change $wpide_review_months; the count in the link is cached for an hour.

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.