Somerussian
Somerussian

Reputation: 391

Wordpress - custom post status is not showing

I've created new post status. Here's code from my custom plugin:

add_action('init', 'new_post_status_add');

function new_post_status_add () {
    register_post_status('refused', array(
        'label'                     => _x('Refused', 'post'),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop('Refused <span class="count">(%s)</span>', 'Refused <span class="count">(%s)</span>'),
    ));
}

But it's not working - not visible in edit form and quick edit form: enter image description here

What should I do to make status available?

Upvotes: 3

Views: 2883

Answers (3)

samjco-com
samjco-com

Reputation: 409

Try adding PHP_INT_MAX to your j_custom_post_status action

add_action( 'init', 'j_custom_post_status', PHP_INT_MAX  );

Upvotes: -1

Raham
Raham

Reputation: 133

Sorry for delayed reply. I hope my comment would help others.

Custom Post Status is an unsolved issue that has been there for such a long time, or at least since 8yrs ago on Wordpress website here.

Wordpress noted on codex document for function register_post_status():

This function does NOT add the registered post status to the admin panel. This functionality is pending future development. Please refer to Trac Ticket #12706. Consider the action hook post_submitbox_misc_actions for adding this parameter.

There is a work around that I could use for one of my projects. I mixed solutions that described on this url and this url.

The summary is that Wordpress doesn't show custom post status automatically. It needs to be manipulated by using your theme's function.php.

First step

Create custom post status, same as you mentioned. just, don't forget that "This function should not be called before the 'init' action."

‌
function j_custom_post_status() {
$args = array(
    'label' => _x( 'Refused', 'post'),
    'label_count' => _n_noop( 'Refused <span class="count">(%s)</span>',  'Refused (%s)', 'post' ),
    'public' => false,
    'internal' => true,
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'exclude_from_search' => false,
);
register_post_status( 'j_refused', $args );

add_action( 'init', 'j_custom_post_status', 0 );
‌

I use WP Generator to get the code snippet above.

Second step

Tweak Admin Panel to show the custom status in Quick Edit Menu (QE) on posts list page edit.php. We need JQuery's help to do so and we need to add JQuery code to Admin Panel footer.

‌
function j_append_status_list(){

    //array of all custom status that you created
    $arr_customstatus = array(
        'refused',
        'other_custom_status',
        );

     echo '<script>';

     //flush
     $tmp = '';

     //generate string
     foreach ($arr_customstatus as $pkey ) {
         $tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
     }

     echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
});";

     echo '</script>';

}
add_action('admin_footer-edit.php','j_append_status_list');
‌

Note: The function above doesn't automatically select the status in QE in case the post's status has been modified before. To add the selected="true" to the option, you need to use the global $post and check the current status, same as the the method used in the Third step below.

Third step

Now, we need to add custom post status meta to post edit page too.

function j_custom_status_metabox(){

    global $post;

    $custom  = get_post_custom( $post->ID );
    $status  = $custom["_status"][0];

    // Array of custom status messages
    $arr_customstatus = array(
        'refused',
        'other_custom_status',
        );

        //Flush
        $tmp = '';

        foreach( $jstats as $pkey ) {

            if( $status == $pkey ){
                 $tmp.= '<option value="'.$pkey.'" selected="true">'. $pkey .'</option>';
             } else {
                 $tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
             }
         }

     echo '<script>';

     echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
});";

     echo '</script>';

}
add_action('admin_footer-post.php','j_custom_status_metabox');
add_action('admin_footer-post-new.php','j_custom_status_metabox');
 ‌

Step four

In case you like to show a message in posts lists based on custom post status, you can add this function too. However, this is optional.

function j_display_status_label( $statuses ) {

    global $post; 

    //Display label on all posts list but not on the custom status list
    if( get_query_var( 'post_status' ) != 'refused' ){ 
        if( $post->post_status == 'refused' ){ 
            return array('Refused'); 
        }
    }
    return $statuses; 
}

add_filter( 'display_post_states', 'j_display_status_label' );
‌‌    ‌

Note: The function above doesn't include all future custom status. You need to add foreach loop in case you need.

Upvotes: 3

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

Try to use this code,

// Register Custom Status
function custom_post_status() {

    $args = array(
        'label'                     => _x( 'Refused', 'Status General Name', 'text_domain' ),
        'label_count'               => _n_noop( 'Refused (%s)',  ' (%s)', 'text_domain' ), 
        'public'                    => true,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'exclude_from_search'       => false,
    );
    register_post_status( 'refused', $args );

}
add_action( 'init', 'custom_post_status', 0 );

I hope this will work for you.

Upvotes: 0

Related Questions