WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
share|improve this question
up vote 4 down vote accepted

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' );
share|improve this answer
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 Feb 3 '13 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 Feb 3 '13 at 1:09
Excellent, this worked! Thanks much. – Eckstein Feb 3 '13 at 5:55
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) – Simon Pollard Feb 2 at 10:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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