"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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.