Over a million developers have joined DZone.

Register New Post Statuses In WordPress

· Web Dev Zone

WordPress posts allow you to have some form of workflow when editing your posts. This is done by assigning a post status for each stage of the WordPress posts.

By default WordPress will take up the status of Draft which tells WordPress to create a post but not to publish it on the front-end of the site, it will only publish it on the site when you hit the publish button and the post status is changed to publish.

Default Statuses

The default WordPress statuses are:

  • Publish - Viewable to public users on the front-end of the site.
  • Future - Posts which are scheduled to be published on a future date.
  • Draft - Incomplete post only viewable to registered users with the correct capability.
  • Pending - Post has been completed but awaiting someone with publish capabilities to publish the post.
  • Private - Post can only be viewed by admin users.
  • Trash - Posts which have been deleted.
  • Auto-Draft - WordPress will auto save drafts.
  • Inherit - Used by child posts such as attachments to determine the post status of the parent.

Create Custom Post Statuses

If you want to create your own post statuses to create your own workflow then WordPress allows you to register your own post statuses to use in your application.

The function you need to use is register_post_status on the init action of your WordPress application.

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

The problem that you will find with this function is that WordPress will not these statuses to the post status dropdown on the edit posts page.

There is a trac ticket open for this problem but it has been open for 5 years so not sure when this will be put in, to view the ticket status you can see it on the below link.

Trac Ticket #12706

To add these to the post status dropdown you need to do this yourself with a bit of JavaScript you can add using the post_submitbox_misc_actions action.

function add_to_post_status_dropdown()
{
    ?>
    <script>
    jQuery(document).ready(function($){
        $("select#post_status").append("<option value=\"archive\" <?php selected('archive', $post->post_status); ?>>Archive</option>");
    });
    </script>
    <?php
}

add_action( 'post_submitbox_misc_actions', 'add_to_post_status_dropdown');

The above function will add some JavaScript to the page which will add the Archive post status to the dropdown, allowing you to choose the new post status.

Get All Available Post Statuses

To return a list of all the available post statuses you can use the function get_post_stati(), which returns an array of all the post statuses.

$postStatuses = get_post_stati();
Topics:

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}