自定义帖子类型的新帖子状态

时间:2013-03-04 作者:dclawson

我有一个自定义的帖子类型recipes. 我正在使用cron脚本自动将新闻聚合到数据库中。

它当前正在导入并另存为“待定审阅”。是否可以创建另一个名为Aggregated 哪个将列出所有要发布的聚合新闻?

我试着用register_post_status 功能,但这似乎不起作用:

function custom_post_status(){
    register_post_status( \'aggregated\', array(
        \'label\'                     => _x( \'Aggregated\', \'recipes\' ),
        \'public\'                    => false,
        \'exclude_from_search\'       => true,
        \'show_in_admin_all_list\'    => true,
        \'show_in_admin_status_list\' => true,
        \'label_count\'               => _n_noop( \'Aggregated <span class="count">(%s)</span>\', \'Aggregated <span class="count">(%s)</span>\' ),
    ) );
}
add_action( \'init\', \'custom_post_status\' );
谢谢你的帮助。

3 个回复
SO网友:Larzan

关于如何做到这一点,这里有一个很好的逐步描述https://www.jclabs.co.uk/create-custom-post-status-in-wordpress-using-register_post_status/

要将自定义帖子状态添加到下拉菜单,只需将以下内容添加到主题功能脚本中:

add_action(\'admin_footer-post.php\', \'jc_append_post_status_list\');
function jc_append_post_status_list(){
 global $post;
 $complete = \'\';
 $label = \'\';
 if($post->post_type == \'recipes\'){
      if($post->post_status == \'aggregated\'){
           $complete = \' selected=\\"selected\\"\';
           $label = \'<span id=\\"post-status-display\\"> Aggregated</span>\';
      }
      echo \'
      <script>
      jQuery(document).ready(function($){
           $("select#post_status").append("<option value=\\"aggregated\\" \'.$complete.\'>Aggregated</option>");
           $(".misc-pub-section label").append("\'.$label.\'");
      });
      </script>
      \';
  }
}
有了这个,你就可以在5分钟内启动并运行你的自定义帖子状态,为我节省了大量时间!

SO网友:vancoder

您的代码应该是健全的,并且应该将所需的状态添加到$wp\\u post\\u statuses全局数组中。

但是,如果您希望它显示在“管理”下拉列表中,这是一个持续存在的问题:https://core.trac.wordpress.org/ticket/12706

SO网友:sudip

“注册帖子状态”;“合计”;对于自定义职位类型;配方:

register_post_status( \'aggregated\', array(
                    \'label\'                     => _x( \'Aggregated \', \'post status label\', \'plugin-domain\' ),
                    \'public\'                    => true,
                    \'label_count\'               => _n_noop( \'Aggregated s <span class="count">(%s)</span>\', \'Aggregated s <span class="count">(%s)</span>\', \'plugin-domain\' ),
                    \'post_type\'                 => array( \'recipes\' ), // Define one or more post types the status can be applied to.
                    \'show_in_admin_all_list\'    => true,
                    \'show_in_admin_status_list\' => true,
                    \'show_in_metabox_dropdown\'  => true,
                    \'show_in_inline_dropdown\'   => true,
                    \'dashicon\'                  => \'dashicons-businessman\',
                ) );
在“中”;“配方”;自定义帖子编辑屏幕的发布元框,在下拉列表中添加自定义帖子状态并更改;“保存草稿”;按钮标签,如果选定的帖子状态为;聚合(&Q):

add_action(\'admin_footer-post.php\',function(){

    global $post;
    $complete = \'\';
    $label = \'\';

    if($post->post_type == \'recipes\') {

        if ( $post->post_status == \'aggregated\' ) {
            $complete = \' selected=\\"selected\\"\';
            $label    = \'Aggregated\';
        }

        $script = <<<SD

 
       jQuery(document).ready(function($){
           $("select#post_status").append("<option value=\\"aggregated\\" \'.$complete.\'>Aggregated</option>");
           
           if( "{$post->post_status}" == "aggregated" ){
                $("span#post-status-display").html("$label");
                $("input#save-post").val("Save Aggregated");
           }
           var jSelect = $("select#post_status");
                
           $("a.save-post-status").on("click", function(){
                
                if( jSelect.val() == "aggregated" ){
                    
                    $("input#save-post").val("Save Aggregated");
                }
           });
      });
     

SD;

        echo \'<script type="text/javascript">\' . $script . \'</script>\';
    }

});
在自定义帖子管理网格的快速编辑屏幕中添加自定义帖子状态:

add_action(\'admin_footer-edit.php\',function() {
    global $post;
    if( $post->post_status == \'recipes\' ) {
        echo "<script>
    jQuery(document).ready( function() {
        jQuery( \'select[name=\\"_status\\"]\' ).append( \'<option value=\\"aggregated\\">Aggregated</option>\' );
    });
    </script>";
    }
});
在自定义帖子管理网格中显示自定义帖子状态合计:

add_filter( \'display_post_states\', function( $statuses ) {
    global $post;

    if( $post->post_type == \'recipes\') {
        if ( get_query_var( \'post_status\' ) != \'aggregated\' ) { // not for pages with all posts of this status
            if ( $post->post_status == \'aggregated\' ) {
                return array( \'Aggregated\' );
            }
        }
    }
    return $statuses;
});

结束

相关推荐