I am using this following code in my functions file to hide and add some custom columns to my post-edit screen in wp-admin.
I am now trying to get the post list to sort by a post meta field (last name). I’ve read through many tutorials on how to do this, but I can’t find anything that matches what I have.
I don’t need the column to be sortable, I just want the list to automatically sort itself by a custom meta key. And, just FYI, I’m not using a custom post type. This just the regular post type.
Can somebody shove me in the right direction on how to do this?
//Add a First and Last Name column to the post edit table
function topo_modify_post_table( $column ) {
$column['first_name'] = 'First Name';
$column['last_name'] = 'Last Name';
return $column;
}
add_filter( 'manage_posts_columns', 'topo_modify_post_table' );
function topo_modify_post_table_row( $column_name, $post_id ) {
$custom_fields = get_post_custom( $post_id );
switch ($column_name) {
case 'first_name' :
?><a style="font-weight:bold;" href="<?php echo admin_url(); ?>post.php?post=<? echo get_the_ID(); ?>&action=edit"><?php the_field('actor-first-name'); ?></a><?php
break;
case 'last_name' : ?>
<a style="font-weight:bold;" href="<?php echo admin_url(); ?>post.php?post=<? echo get_the_ID(); ?>&action=edit"><?php the_field('actor-last-name'); ?></a><?php
break;
default:
}
}
add_filter( 'manage_posts_custom_column', 'topo_modify_post_table_row', 10, 2 );
//Remove columns
add_filter('manage_post_posts_columns', 'ST4_columns_remove_category');
// REMOVE DEFAULT COLUMNS
function ST4_columns_remove_category($defaults) {
// to get defaults column names:
// print_r($defaults);
unset($defaults['comments']);
unset($defaults['date']);
unset($defaults['author']);
unset($defaults['title']);
return $defaults;
}
Something like this should work:
Instead of relying on the
$_GET
variables, theorderby
value is also passed in the$query
class itself. So you could also write it like this: