Displaying Pending Posts in WordPress: A Simple Hook Solution
WordPress's powerful hook system allows developers to easily customize its functionality. One common request is to display pending posts instead of published ones, perhaps for review purposes or to showcase draft content. This article guides you through using a simple hook to achieve this.
The Problem: Displaying Pending Posts
Imagine a scenario where you want to create a dedicated section on your WordPress site to review and approve draft posts before making them public. You want to see the latest pending posts, but the default WordPress loop only displays published content.
The Solution: A Custom Hook
Let's utilize the pre_get_posts
hook, which allows us to modify the main query before it executes. This hook fires before WordPress retrieves posts and lets you control which posts are displayed.
Here's the code snippet:
add_action( 'pre_get_posts', 'display_pending_posts' );
function display_pending_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
$query->set( 'post_status', 'pending' );
}
This code adds a hook to modify the main query. It ensures the query only targets pending posts (post_status
set to 'pending').
Dissecting the Code:
-
add_action( 'pre_get_posts', 'display_pending_posts' );
: This line registers thedisplay_pending_posts
function to be executed when thepre_get_posts
action is triggered. -
function display_pending_posts( $query )
: This function accepts the$query
object as an argument, allowing us to manipulate the query settings. -
if ( is_admin() || ! $query->is_main_query() ) { return; }
: This conditional statement ensures the function only executes on the main query (not in the admin area) and prevents unintended behavior on other query types. -
$query->set( 'post_status', 'pending' );
: This line is the core of the modification. It sets thepost_status
parameter of the query to 'pending', ensuring the loop only retrieves posts in this specific status.
Adding Functionality:
You can further customize this code to achieve specific requirements:
- Filter by Post Type: Use
$query->set( 'post_type', 'your_custom_post_type' );
to target pending posts of a specific post type. - Display a Specific Number of Posts: Use
$query->set( 'posts_per_page', 5 );
to show only the top 5 pending posts.
Implementing the Hook:
You can implement this code by:
- Adding it to your
functions.php
file: This is the most common method, allowing you to integrate it directly into your theme's functionality. - Using a plugin: Create a custom plugin to house the code, ensuring better organization and easier management.
- Creating a custom template file: For more granular control, create a dedicated template file (e.g.,
pending-posts.php
) and include the code within it.
Conclusion:
By leveraging the pre_get_posts
hook, you can effectively display pending posts on your WordPress site. This simple, yet powerful solution allows you to implement custom workflows and control the displayed content, making your website more dynamic and functional. Remember to tailor this code according to your specific needs and enjoy the flexibility of WordPress's hook system.