Sorting custom columns in admin interface – I'm *almost* there
-
Hello folks,
– I have a custom field (via the ACF plugin) called Counts.
– It has a Field Type set to “numbers”, because it stores numbers. D’uh.
– I managed to have it show up as a new column in the “Posts -> All Posts” view on the admin interface (/wp-admin/edit.php). The column correctly indicates the Counts value for each post, which is great. This is derived from Eliot Akira’s post.
– The column is sortable. Because my end objective is to order all my posts by their Counts value (asc. or desc.)
– But right now clicking on the sort button sorts the posts… by their age or ID number (e.g. the first posts I created come up, or the latest posts) rather than by their Counts value (??)
I’m out of energy after having come this far with my meagre skills, though. 🙁 Somebody help me with the last few steps ?
Rachel Carden at WPDreamer has something worked out, but the meta_key thing defeats me.
My current function :
////////////////////////////// //Add custom column using the Counts field to the posts admin display ////////////////////////////// add_filter('manage_edit-post_columns', 'my_columns_head'); function my_columns_head($defaults) { $defaults['Counts'] = 'Views'; return $defaults; } //Add rows data add_action( 'manage_post_posts_custom_column' , 'my_custom_column', 10, 2 ); function my_custom_column($column, $post_id ){ switch ( $column ) { case 'Counts': $views_value = get_field( 'field_5465eef2489a9', $post_id ); echo $views_value; break; } } // Make these columns sortable function sortable_columns() { return array( 'Counts' => 'Counts' ); } add_filter( "manage_edit-post_sortable_columns", "sortable_columns" );
The topic ‘Sorting custom columns in admin interface – I'm *almost* there’ is closed to new replies.