Custom post status not appearing

2019-02-13 16:16发布

问题:

I am building a directory theme for a client of mine, and I like to add the feature of expiration in the posts by modifying the post status from publish to expired.

To achieve that, I am trying to register a new post status by using the following code:

add_action('init',    'registerStatus', 0);

function registerStatus()
{
    $args = array(
        'label'                     =>  _x('Expired', 'Status General Name', 'z' ),
        'label_count'               =>  _n_noop('Expired (%s)',  'Expired (%s)', 'z'),
        'public'                    =>  true,
        'show_in_admin_all_list'    =>  true,
        'show_in_admin_status_list' =>  true,
        'exclude_from_search'       =>  true
    );

    register_post_status('expired', $args);
}

The problem is that I cannot see the registered post status either in WordPress posts, either in my custom post type post statuses.

Am I doing something wrong?

回答1:

Custom post status functionality is still under development (as it has been for the past four years!), see https://core.trac.wordpress.org/ticket/12706, and comments on https://wordpress.stackexchange.com/q/67655/25765. More useful info here: https://wordpress.stackexchange.com/search?q=register_post_status.

Personally, I'd strongly discourage implementing custom post statuses, but if really necessary, you could take a look at how the Edit Flow plugin handles it.



回答2:

Thanks to Ryan Bayne I was able to add a custom post status to the admin panel on the edit post page. There is no wordpress filter avaiable. His solution with jQuery is perfect. Here the code if anyone else is searching for a solution:

add_action( 'post_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
    function my_post_submitbox_misc_actions(){

    global $post;

    //only when editing a post
    if( $post->post_type == 'post' ){

        // custom post status: approved
        $complete = '';
        $label = '';   

        if( $post->post_status == 'approved' ){
            $complete = 'selected=\"selected\"';
            $label = '<span id=\"post-status-display\"> Approved</span>';
        }

        echo '<script>'.
                 'jQuery(document).ready(function($){'.
                     '$("select#post_status").append('.
                         '"<option value=\"approved\" '.$complete.'>'.
                             'Approved'.
                         '</option>"'.
                     ');'.
                     '$(".misc-pub-section label").append("'.$label.'");'.
                 '});'.
             '</script>';
    }
}


回答3:

This feature is still pending for future development

NOTICE: 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.



回答4:

Now November 2014 and still issues with custom statuses. I think the original code posted is fine. Here is a video showing an issue you will run into when implementing custom post status though. There may be a workaround i.e. hooking into the posts query and doing a custom query but I've not started the research.

Screencast of posts not showing in the All table when a custom status is applied, however the posts can be found in the table view for each custom status. Click here to view short clip.

That screencast was taking while I worked on my new WTG Tasks Manager plugin. I will leave my design in the plugin as it is and hopefully it helps to encourage improvements in this area of WordPress.

For proper answer...my custom status do show on the Edit Post screen for my custom post type so that is possible. If you want to take a look at my plugins registration of custom post type and statuses go to directory "posttypes/tasks.php" and play around with a working example. Here is the plugins official page...

https://wordpress.org/plugins/wtg-tasks-manager/