How can I change the order of posts in the admin dashboard, so they display alphabetically according to title, rather than latest first?
3 Answers
If you don't wish to always click the "Title" column to sort your posts by title, you can place this code in either your currently active WordPress theme's functions.php
file, or within a plugin. This will automatically always sort your posts for you, so you don't have to click the title column every time.
You can use this for setting default sort order on post types.
/* 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');
}
You can use some of these example conditions...
/* 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'){
}
If you wanted to apply this sorting on ALL post types, regardless of whether or not they are "built-in"...
Change this:
$post_types = get_post_types(array('_builtin' => true), 'names');
To this:
$post_types = get_post_types('', 'names');
-
Is it ok to use within the function rather than check before the action
if ( ! is_admin ) { return; }
– urok93Commented Sep 30, 2012 at 21:07 -
-
You must add a "return $query;" before function end, otherwise this will not work in later wordpress editions.– JobstCommented Apr 20, 2017 at 2:38
-
I think a plugin is running this function and overriding my custom function. Is there a hook to ensure that my code is run rather than the plugins? Commented Aug 20, 2018 at 11:23
function custom_field() {
add_meta_box(
'post_priority',
'Post Priority',
'priority_meta_box',
'post',
'normal',
'low'
);
}
add_action('add_meta_boxes', 'custom_field');
function priority_meta_box($post) {
$priority = get_post_meta($post->ID, 'post_priority', true);
?>
<label for="post_priority">Post Priority:</label>
<input type="number" id="post_priority" name="post_priority" value="<?php echo $priority; ?>" min="1">
<?php
}
// Save the custom field value
function handle_priority_submit($post_id) {
if (array_key_exists('post_priority', $_POST)) {
$priority = $_POST['post_priority'];
if ($priority >=1){
update_post_meta($post_id,'post_priority',$priority);
}
}
}
add_action('save_post','handle_priority_submit');
function sort_posts_by_priority_in_admin($query) {
if ($query->get('post_type') === 'post') {
$query->set('meta_key', 'post_priority');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'ASC');
}
}
add_action('pre_get_posts', 'sort_posts_by_priority_in_admin');
// for addingcolumn in admin post area (adding column in admin post area stack exchange)
add_filter('manage_posts_columns', 'add_priority_column');
function add_priority_column($columns) {
$columns['post_priority'] = 'Priority';
return $columns;
}
add_action('manage_posts_custom_column', 'priority_column_value', 10, 2);
function priority_column_value($column, $post_id) {
if ($column == 'post_priority') {
$priority = get_post_meta($post_id, 'post_priority', true);
echo esc_html($priority ? $priority : '—');
}}