Answers:
如果您不希望总是单击“标题”列以标题对帖子进行排序,则可以将此代码放置在当前活动的WordPress主题的functions.php
文件中或插件中。这将始终为您自动排序帖子,因此您不必每次都单击标题栏。
您可以使用它来设置帖子类型的默认排序顺序。
/* Sort posts in wp_list_table by column in ascending or descending order. */
function custom_post_order($query){
/*
Set post types.
_builtin => true returns WordPress default post types.
_builtin => false returns custom registered post types.
*/
$post_types = get_post_types(array('_builtin' => true), 'names');
/* The current post type. */
$post_type = $query->get('post_type');
/* Check post types. */
if(in_array($post_type, $post_types)){
/* Post Column: e.g. title */
if($query->get('orderby') == ''){
$query->set('orderby', 'title');
}
/* Post Order: ASC / DESC */
if($query->get('order') == ''){
$query->set('order', 'ASC');
}
}
}
if(is_admin()){
add_action('pre_get_posts', 'custom_post_order');
}
您可以使用其中一些示例条件...
/* Effects all post types in the array. */
if(in_array($post_type, $post_types)){
}
/* Effects only a specific post type in the array of post types. */
if(in_array($post_type, $post_types) && $post_type == 'your_post_type_name'){
}
/* Effects all post types in the array of post types, except a specific post type. */
if(in_array($post_type, $post_types) && $post_type != 'your_post_type_name'){
}
如果要对所有帖子类型应用此排序,无论它们是否为“内置” ...
更改此:
$post_types = get_post_types(array('_builtin' => true), 'names');
对此:
$post_types = get_post_types('', 'names');
if ( ! is_admin ) { return; }