0

My site has two custom types: Auction and Lot. Lots are connected to an Auction via the post_parent field. I added an Auction column to the Lots list in the admin area.

function theme_lot_custom_columns($columns){
    $new = array();
    foreach($columns as $key => $value) {
        if ($key=='date') {
            // Put the Auction column before the Date column
            $new['auction'] = __('Auction');
        }
        $new[$key] = $value;
    }
return $new;
}
add_filter('manage_lot_posts_columns', 'theme_lot_custom_columns');

function theme_lot_custom_column($column, $post_id) {
    if ($column === 'auction'){
        $post = get_post($post_id);
        if ($post->post_parent) {
            $post_parent = get_post($post->post_parent);
            echo '<a href="' . get_edit_post_link($post_parent->ID). '">' . $post_parent->post_title . '</a>';
        }
    }
}
add_action('manage_lot_posts_custom_column', 'theme_lot_custom_column', 5, 2);

I'd like to make this Auction column sortable alphabetically by the auction title. I thought that adding the following code was enough, but no.

function theme_lot_sortable_columns($columns) {
    $columns['auction'] = 'auction';
    return $columns;
}   
add_filter('manage_edit-lot_sortable_columns', 'theme_lot_sortable_columns');

Any help would be appreciated.

Thanks!

0

I believe the missing piece to your puzzle is one additional filter to perform the sort and return the sorted data. Try something like the following:

function sort_column_by_auction_1357( $vars ){
if ( isset( $vars["orderby"] ) && "auction" == $vars["orderby"] ) {
    $vars = array_merge( $vars, array(
        "orderby" => "auction"
    ) );
}
return $vars;
}
add_filter( "request", "sort_column_by_auction_1357" );

This worked for me. I added a sort by Last Modified to all of my custom post type views.

  • I've tried your suggested code to no effect. I think what I'm trying to do is not possible unless I store the post parent title in a meta field, so it can be referenced in the sort_column_by_auction_1357 function. – leemon Sep 27 '13 at 6:50
0

Try this code

add_action( 'pre_get_posts', 'as25_column_orderby' );
function as25_column_orderby( $orderby ) {
 if( ! is_admin() || 'lot' != $query->get( 'post_type' ) )
    return;

 $orderby = $query->get( 'orderby' );

  if( 'auction' == $orderby ) {
   $query->set( 'orderby', 'parent' );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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