How to Add Custom Post Status for Blog Posts in WordPress

5 hours ago, WordPress Tutorials, Views
How to add custom post status for blog posts in WordPress

Understanding Custom Post Statuses in WordPress

WordPress, by default, provides a few standard post statuses like “Published,” “Draft,” “Pending Review,” and “Private.” These statuses help you manage your content workflow, allowing you to track the progress of your blog posts from creation to publication. However, these default statuses might not always be sufficient for more complex editorial processes. This is where custom post statuses come in handy.

Custom post statuses allow you to define new states for your posts, offering greater flexibility in organizing and managing your content. For example, you might want to create a “Proofreading” status to indicate that a post is currently being reviewed by an editor, or a “Scheduled” status to differentiate between future posts. This level of customization can significantly improve your team’s efficiency and overall content management.

Why Use Custom Post Statuses?

Implementing custom post statuses can streamline your editorial workflow and provide a more granular view of your content’s progress. Here are a few key benefits:

  • Improved Content Organization: Custom statuses allow you to categorize your content based on specific stages of development or review.
  • Enhanced Team Collaboration: By defining clear statuses, team members can easily understand the current state of a post and what actions are required.
  • Streamlined Workflow: Custom statuses can automate tasks based on the post’s current state.

Beyond these benefits, custom statuses also help in content filtering and reporting. For instance, you can easily filter posts by a specific status to identify content that requires immediate attention.

Methods for Adding Custom Post Statuses

There are several ways to add custom post statuses to your WordPress website. You can use code snippets directly within your theme’s `functions.php` file (not recommended for beginners or long-term maintenance), create a custom plugin, or leverage existing plugins designed for this purpose. Each method has its pros and cons, which we’ll explore below.

Adding Custom Post Statuses with Code

This method involves adding PHP code to your theme’s `functions.php` file or, ideally, creating a custom plugin. This approach provides the most control but requires a good understanding of PHP and WordPress development practices. It’s crucial to understand the risks of directly modifying the `functions.php` file, such as potential site crashes due to errors.

Here’s a step-by-step guide:

  1. Create a custom plugin (recommended): Create a new folder in the `wp-content/plugins/` directory. Inside that folder, create a PHP file (e.g., `custom-post-status.php`). This file will house your custom code.
  2. Add the plugin header: Add the necessary plugin header information to the PHP file (plugin name, description, author, version, etc.). This tells WordPress that this is a valid plugin.
  3. Use the `register_post_status` function: Use the `register_post_status` function within an action hook, usually `init`. This function registers your custom post status with WordPress.
  4. Add the status to the post status dropdown: You’ll also need to add your custom status to the post status dropdown in the editor. This involves using the `display_post_states` filter.

Here’s an example code snippet (remember to adapt it to your specific needs and use a custom plugin):


<?php
/*
Plugin Name: Custom Post Status
Description: Adds a custom post status to WordPress.
Version: 1.0.0
Author: Your Name
*/

add_action( 'init', 'register_my_post_status' );
function register_my_post_status() {
  register_post_status( 'proofreading', array(
    'label'                     => _x( 'Proofreading', 'post' ),
    'public'                    => false,
    'exclude_from_search'       => true,
    'show_in_admin_all_list'    => true,
    'show_in_admin_status_list' => true,
    'label_count'               => _n_noop( 'Proofreading <span class="count">(%s)</span>', 'Proofreading <span class="count">(%s)</span>' ),
  ) );
}

add_filter( 'display_post_states', 'add_proofreading_post_status' );
function add_proofreading_post_status( $post_states ) {
  global $post;
  $arg = get_query_var( 'post_status' );
  if ( $arg != 'proofreading' ) {
    if ( $post->post_status == 'proofreading' ) {
      $post_states[] = __( 'Proofreading' );
    }
  }
  return $post_states;
}

add_action( 'admin_footer-post.php', 'add_proofreading_to_status_dropdown' );
function add_proofreading_to_status_dropdown(){
    global $post;
    $complete = '';
    $label = 'Proofreading';
    if($post->post_status == 'proofreading'){
        $complete = ' selected="selected"';
    }
    echo '<script>
    jQuery(document).ready(function($){
        $("select#post_status").append("<option value="proofreading" '.$complete.'>'.$label.'</option>");
    });
    </script>';
}


add_action( 'admin_footer-edit.php', 'add_bulk_proofreading_to_status_dropdown' );
function add_bulk_proofreading_to_status_dropdown() {
    echo "<script>
        jQuery(document).ready(function($) {
            $('<option value="proofreading">Proofreading</option>').appendTo("select[name='_status']");
        });
    </script>";
}

?>

Explanation of the code:

  • `register_post_status()`: Registers the new post status “proofreading.” The `label` argument sets the display name, `public` is set to `false` because it’s not a public-facing status, `exclude_from_search` prevents it from appearing in search results, and `show_in_admin_all_list` and `show_in_admin_status_list` ensure it appears in the admin interface.
  • `display_post_states` filter: Adds the “Proofreading” label to the post status list in the admin area when viewing a post.
  • `admin_footer-post.php` action: Modifies the post status dropdown on the post editing screen to include the “Proofreading” status. It dynamically selects the option if the current post status is “proofreading”.
  • `admin_footer-edit.php` action: Modifies the bulk edit dropdown on the posts list screen to include the “Proofreading” status.

Important Considerations:

  • Always create a backup of your `functions.php` file or your entire website before making any changes.
  • Test the code thoroughly in a staging environment before deploying it to your live website.
  • Use a child theme if you are modifying your theme’s `functions.php` file to prevent losing your changes during theme updates.

Using Plugins to Add Custom Post Statuses

If you’re not comfortable with coding, using a plugin is the recommended approach. Several plugins are available in the WordPress repository that simplify the process of adding and managing custom post statuses. These plugins often provide a user-friendly interface and additional features, making it easier to customize your workflow.

Here are some popular plugins:

  • Custom Post Status: A straightforward plugin dedicated to adding custom post statuses.
  • PublishPress Series: PublishPress Series (and its add-ons) can manage custom statuses as part of its series functionality.
  • Edit Flow: A more comprehensive editorial workflow plugin that includes custom statuses as part of its feature set.

To install a plugin, go to the “Plugins” section in your WordPress admin dashboard, click “Add New,” search for the plugin, install it, and activate it.

Plugin Configuration:

Each plugin has its own configuration options. Typically, you’ll find a settings page in the WordPress admin dashboard where you can define your custom post statuses, their labels, and other properties. Follow the plugin’s documentation for specific instructions.

Displaying Custom Post Statuses in the Admin Area

Once you’ve added your custom post statuses, you’ll want to ensure they are displayed correctly in the WordPress admin area. This involves adding them to the post status dropdown in the editor and making them visible in the post list table.

The code example provided above already handles adding the status to the dropdown using JavaScript. The `register_post_status` function also configures whether the status shows in various admin lists. However, if you are using a plugin, it typically handles this automatically.

Filtering Posts by Custom Status

Being able to filter your posts by their custom status is a crucial aspect of effective content management. WordPress allows you to filter posts based on their status in the admin area. You can also create custom queries to retrieve posts with specific statuses for display on your website.

Filtering in the Admin Area:

In the “Posts” section of the WordPress admin dashboard, you should see a dropdown menu that allows you to filter posts by their status. Your custom post statuses should appear in this dropdown, allowing you to quickly find posts in a particular state.

Custom Queries:

To display posts with a specific custom status on your website, you can use a custom WordPress query. Here’s an example:


<?php
$args = array(
  'post_type' => 'post',
  'post_status' => 'proofreading', // Replace 'proofreading' with your custom status
  'posts_per_page' => -1, // Retrieve all posts
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    $query->the_post();
    // Display the post information
    echo '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';
    echo '<p>' . get_the_excerpt() . '</p>';
  }
  wp_reset_postdata();
} else {
  echo '<p>No posts found with the status "Proofreading".</p>';
}
?>

Explanation:

This code snippet creates a new WordPress query that retrieves all posts of the type “post” with the status “proofreading.” It then loops through the results and displays the title and excerpt of each post. Remember to replace `”proofreading”` with the actual name of your custom post status.

Conclusion

Adding custom post statuses to WordPress provides a powerful way to tailor your content workflow to your specific needs. Whether you choose to use code snippets or plugins, the benefits of enhanced organization, improved team collaboration, and streamlined processes are significant. By understanding the different methods and considerations involved, you can effectively implement custom post statuses and optimize your WordPress content management strategy.