Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Last active September 19, 2023 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterwilsoncc/03356677f2ce754234be517bb2f2aa15 to your computer and use it in GitHub Desktop.
Save peterwilsoncc/03356677f2ce754234be517bb2f2aa15 to your computer and use it in GitHub Desktop.
Allow administrators to view all posts on the posts archive page.
<?php
/**
* Plugin Name: Unlimited Posts For Admins
* Description: Allow administrators to view all posts on the posts archive page.
* Version: 1.0.0
* Update URI: false
* Plugin URI: https://aus.social/@peterwilsoncc/111094215185643333
*/
namespace PWCC\UnlimitedPostsForAdmins;
/**
* Parse the querystring parameter to allow viewing all posts.
*
* This function is hooked into the `pre_get_posts` action.
*
* Allow admins to view all posts on archive pages by appending
* `?unlimited` to the URL.
*
* @param WP_Query $wp_query The query object.
*/
function parse_querystring_parameter( $wp_query ) {
if ( is_admin() || ! $wp_query->is_main_query() || ! $wp_query->is_archive() ) {
return;
}
/*
* Only allow users with the export capability to view all posts.
*
* This capability isn't perfect but listing all posts is a form of
* export so it's a close enough match. By default, only site admins
* have this capability.
*/
if ( ! current_user_can( 'export' ) ) {
return;
}
if ( ! isset( $_GET['unlimited'] ) ) {
return;
}
$wp_query->set( 'posts_per_page', -1 );
}
add_action( 'pre_get_posts', __NAMESPACE__ . '\\parse_querystring_parameter' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment