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
        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


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');

function ST4_columns_remove_category($defaults) {
    // to get defaults column names:
    // print_r($defaults);
    return $defaults;

2 Answers 2


Something like this should work:

function wpa84258_admin_posts_sort_last_name( $query ){
    global $pagenow;
    if( is_admin()
        && 'edit.php' == $pagenow
        && !isset( $_GET['orderby'] )
        && !isset( $_GET['post_type'] ) ){
            $query->set( 'meta_key', 'last_name' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
add_action( 'pre_get_posts', 'wpa84258_admin_posts_sort_last_name' );
  • Thanks for your help! This seems close to what I need, but when I add it, the posts don't list at all and the first name/last name fields disappear as headings.
    – Eckstein
    Commented Feb 3, 2013 at 0:06
  • oh, I didn't notice your meta key isn't last_name, you'll have to change it match your meta key, actor-last-name.
    – Milo
    Commented Feb 3, 2013 at 1:09
  • Highly recommend adding "$query->is_main_query()" to that list as well - in case you need to run any other queries in your columns (I needed to) Commented Feb 2, 2016 at 10:35

Instead of relying on the $_GET variables, the orderby value is also passed in the $query class itself. So you could also write it like this:

add_action('pre_get_posts', function($query) {
    if ($query->query['orderby'] == "actor-last-name") {
        $query->set('meta_key', 'actor-last-name');
        $query->set('orderby', 'meta_value');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.