user4831663
user4831663

Reputation:

WP Admin Custom ID Column 'sortable' function not working

I have added a new column to the Wordpress post admin overview that displays each post's ID, I have also added what I believe is the correct code for making the column sortable but is not working.

Can anyone see a problem with my sorting function below? I should say the column is being registered fine and I can see all post ID's as expected. It is just the sorting of the column that is not working.

add_filter( 'manage_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_posts_custom_column', 'revealid_id_column_content', 5, 2 );


// Register column
function revealid_add_id_column( $columns ) {
   $columns['revealid_id'] = 'ID';
   return $columns;
}

// Add column content, in this case Post ID
function revealid_id_column_content( $column, $id ) {
  if( 'revealid_id' == $column ) {
    echo $id;
  }
}

// Make Column Sortable (Note: This is NOT working)
add_filter( 'manage_posts_sortable_columns', 'sortable_id_column' );
function sortable_id_column( $columns ) {
    $columns['revealid_id'] = 'ID';
    return $columns;
}

Upvotes: 1

Views: 992

Answers (1)

tschutter
tschutter

Reputation: 131

The filter you are using for sorting doesn't exist, you have to use 'manage_edit-post_sortable_columns'. Here is a working example:

add_filter( 'manage_edit-post_sortable_columns', 'sortable_id_column' );

function sortable_id_column( $columns ) {
    $columns['revealid_id'] = 'ID';

    return $columns;
}

Upvotes: 3

Related Questions