Tutorial: Creating Custom Post Statuses

Custom Post Statuses are at the heart of any web application built with WordPress, and Piklist makes it easy for you to implement them.  No longer are you stuck with draft, pending review and published.  You can add whatever post statuses you choose.

A few important caveat’s:

  • Due to the nature of WordPress, your first post status has to remain “draft“.  You can change the label so it displays “New“, or “Howdy“, but the key will have to remain draft (will explain more later in the tutorial)
  • Some themes may not display your post if the status is not published, so you may have to modify your theme’s loop to show it.
  • This parameter does not ADD statuses to the Post Type, it removes the default WordPress statuses and REPLACES them.

 

In this tutorial we’ll add some custom post statuses to a custom post type.

  1. First we need to create our Custom Post Type. We are going to use our base tutorial for creating Custom Post Types as a starting point.
  2. Like most items in PIklist, we will use an array of values to add our Custom Post Statuses. Piklist has a bunch of parameters to use with Custom Post Types, the one we will use is status.  Add this code to the Custom Post Type you built in step 1:
    ,'status' => array(
       'draft' => array(
         'label' => 'New'
       )
    )

    This code tells Piklist to rename the draft status to New. Due to the nature of WordPress, your first status must always be draft. We just renamed it to New.

  3. The second post status just gets added to this array. The “Request for Estimate” status looks like this:
    ,'status' => array(
       'draft' => array(
         'label' => 'New'
       )
       ,'estimate' => array(
         'label' => 'Request for Estimate'
    )

    You’ve just created post status named “estimate“, with the label “Request for Estimate“.

     

  4. Just continue adding your statuses to the array until you’re done. Here’s the full code snippet for creating the Custom Post Type from step 1, and adding in all the post statuses from the image above:
    add_filter('piklist_post_types', 'demo_post_type');
     function demo_post_type($post_types)
     {
      $post_types['demo'] = array(
        'labels' => piklist('post_type_labels', 'Demo')
        ,'public' => true
        ,'rewrite' => array(
          'slug' => 'demo'
        )
        ,'supports' => array(
          'author'
          ,'revisions'
        )
        ,'hide_meta_box' => array(
          'slug'
          ,'author'
          ,'revisions'
          ,'comments'
          ,'commentstatus'
        )
        ,'status' => array(
           'draft' => array(
             'label' => 'New'
           )
           ,'estimate' => array(
             'label' => 'Request for Estimate'
           )
           ,'prequote' => array(
             'label' => 'Pre-Quote'
           )
           ,'preinspection' => array(
             'label' => 'Preinspection'
           )
           ,'inspection' => array(
             'label' => 'Inspection'
           )
           ,'repair-to-quote' => array(
             'label' => 'Waiting to Quote'
           )
           ,'repair-quote' => array(
             'label' => 'Quoted'
           )
           ,'repair' => array(
             'label' => 'Quote Approved'
           )
           ,'repair-queue' => array(
             'label' => 'Repair Queue'
           )
           ,'repair-declined' => array(
             'label' => 'Repair Complete'
           )
           ,'ready-to-ship' => array(
             'label' => 'Ready to ship'
           )
           ,'closed' => array(
             'label' => 'Closed'
           )
         )
    );
    return $post_types;
    }

Targeting a Post with a custom post status is done with the standard WP_Query class, and it’s post_status parameter. If you want to show all posts from the “demo” custom post type, in any of these statuses: estimate, prequote or preinspection, you can use code like this in your theme or plugin:

$query = new WP_Query(
  array(
   'post_type' => array( 'demo')
   ,'post_status' => array( 'estimate', 'prequote', 'preinspection' ) ) ); 
  )
);