How to Display Recent Drafts from All Authors in WordPress

Home » Snippets » How to Display Recent Drafts from All Authors in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

As a WordPress website manager, keeping track of all the drafts from different authors can be a daunting task, especially if you’re handling a multi-author blog or website. Naturally, you’d want to oversee what content is being produced, who’s working on what, and gauge the progress. Fortunately, there’s an easier way to keep an eye on all the recent drafts from all your authors. This article will guide you on how to effectively manage and display recent drafts from all authors on your WordPress site.

					function wpturbo_recent_drafts_all_authors( $query ) {
    if( $query->is_main_query() && is_post_type_archive( 'draft' ) ) {
         $query->set( 'post_status', 'draft' );
         $query->set( 'post_type', array( 'post', 'page', 'any_other_custom_post_type' ) );
         $query->set( 'posts_per_page', '-1' );
    }
}
add_action( 'pre_get_posts', 'wpturbo_recent_drafts_all_authors' );
				

The given code snippet revolves around the WordPress function pre_get_posts, which is an action that modifies the query before it is executed. In WordPress, actions are functions triggered by specific events occurring in WordPress. This functionality is particularly useful because it provides an opportunity to modify default WP_Query instances, allowing more control over what exactly is displayed on your WordPress site without needing to directly alter template files.

The custom function wpturbo_recent_drafts_all_authors($query) is defined to modify the main query on your draft post archives. The $query variable passed as a parameter is an instance of WP_Query, representing the current query that WordPress is working with.

The conditional logic statement inside the function checks both if the query is the main query of the page and if the page is the archive for draft posts.

    if( $query->is_main_query() && is_post_type_archive( 'draft' ) ) {

The is_main_query() function checks to see if the ‘default’ (main) query is being used on the current page. We use it to ensure that we modify only the main query and not any others.

is_post_type_archive( 'draft' ) checks to verify that the current page is an archive for posts of the type ‘draft’.

When both conditions are met, the function modifies the $query instance using the WP_Query’s set method.

$query->set( 'post_status', 'draft' );
$query->set( 'post_type', array( 'post', 'page', 'any_other_custom_post_type' ) );
$query->set( 'posts_per_page', '-1' );

post_status is set to ‘draft’ to specifically target the drafts regardless of the author. post_type is set to an array of post types, that are considered during the query. By replacing ‘any_other_custom_post_type’ with your custom post types (if any), you can tailor this array to suit your requirements. The code could include ‘post’, ‘page’, or any custom post types defined on your WordPress site.

posts_per_page is set to ‘-1’. This ensures that all drafts will be shown on a single page, regardless of the number of drafts that exist. By default, WordPress splits posts into pages. By stating ‘-1’, we are essentially overriding this default and displaying all posts on one page.

The last part of the code hooks our custom function wpturbo_recent_drafts_all_authors($query) to the pre_get_posts action, so that this function will be called, and our modifications to the query applied, before the posts are fetched.

add_action( 'pre_get_posts', 'wpturbo_recent_drafts_all_authors' );

This ensures that our modified query is utilized when generating the list of ‘drafts’ in the admin view.

Register an account to save your snippets or go Pro to get more features.