如何在仪表板管理中按日期顺序排序自定义帖子类型的帖子?

时间:2013-01-17 作者:Steffi

我创建了一个名为“视频”的新帖子类型。

当我为帖子类型创建帖子时,帖子的顺序是title ASC.

Is it possible to order posts by date DESC please ?

register_post_type(\'Videos\', array(
    \'labels\' => array(
        \'name\' => _x(\'Videos\', \'post type general name\'),
        \'singular_name\' => _x(\'Video\', \'post type singular name\'),
        \'add_new\' => _x(\'Ajouter\', \'Video\'),
        \'add_new_item\' => __(\'Ajouter une video\'),
        \'edit_item\' => __(\'Éditer une video\'),
        \'new_item\' => __(\'Nouvelle video\'),
        \'view_item\' => __(\'Voir le lien de la video\'),
        //\'search_items\' => __(\' Video\'),
        \'menu_name\' => \'Video\'
    ),
    \'public\' => true,
    \'show_ui\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => true,
    \'rewrite\' => array(\'slug\' => \'video\'),
    \'query_var\' => true,
    \'supports\' => array(
        \'title\',
        \'editor\' => false,
        \'excerpt\' => false,
        \'trackbacks\' => false,
        \'custom-fields\',
        \'comments\' => false,
        \'revisions\' => false,
        \'thumbnail\' => false,
        \'author\' => false,
        \'page-attributes\' => false,
    ),
    \'taxonomies\' => array(\'post_tag\')
   )
 );

3 个回复
最合适的回答,由SO网友:Pontus Abrahamsson 整理而成

好的,你可以挂上过滤器pre_get_posts 和检查is_admin.将此内容放入主题或插件中:

function wpse_81939_post_types_admin_order( $wp_query ) {
  if (is_admin()) {

    // Get the post type from the query
    $post_type = $wp_query->query[\'post_type\'];

    if ( $post_type == \'Videos\') {

      $wp_query->set(\'orderby\', \'date\');

      $wp_query->set(\'order\', \'DESC\');
    }
  }
}
add_filter(\'pre_get_posts\', \'wpse_81939_post_types_admin_order\');
我也会将post\\u类型的“Videos”更改为小写,如“video”。

SO网友:cenk

上面的示例通过单击列禁用排序功能。

可排序(&A);对于多个自定义帖子类型:

function wpse_819391_post_types_admin_order( $wp_query ) {
  if ( is_admin() && !isset( $_GET[\'orderby\'] ) ) {     
    // Get the post type from the query
    $post_type = $wp_query->query[\'post_type\'];
    if ( in_array( $post_type, array(\'videos\',\'news\',\'text\') ) ) {
      $wp_query->set(\'orderby\', \'date\');
      $wp_query->set(\'order\', \'DESC\');
    }
  }
}
add_filter(\'pre_get_posts\', \'wpse_819391_post_types_admin_order\');

SO网友:T.Todua

我使用了一种不同的方法:

    add_action(\'pre_get_posts\', \'filter_posts_list\'); 

    function filter_posts_list($query)  {
        //$pagenow holds the name of the current page being viewed
         global $pagenow, $typenow;  
        if(current_user_can(\'edit_posts\') && (\'edit.php\' == $pagenow))  { 
            //global $query\'s set() method for setting
            $query->set(\'orderby\', \'date\');
            $query->set(\'order\', \'desc\');
        }
    }

结束