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!

3 Answers 3

1

To make your custom post type column auction sortable, you also need to handle the sorting logic in the query. You need to use the pre_get_posts action hook to modify the main query.

The theme_lot_custom_orderby function checks if the current query is in the admin area and the main query. If the orderby parameter is set to 'auction,' it modifies it to 'title' for sorting by auction titles.

// Add Auction column to the admin list
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');

// Display content in Auction column
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);

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

// Handle sorting logic
function theme_lot_custom_orderby($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

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

    if ($orderby === 'auction') {
        $query->set('orderby', 'title'); // Assuming auction titles should be used for sorting
    }
}
add_action('pre_get_posts', 'theme_lot_custom_orderby');
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.

1
  • 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
    Commented Sep 27, 2013 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 and acknowledge you have read our privacy policy.

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