WordPress Development Asked by leemon on February 15, 2021
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!
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' );
}
}
Answered by sabarnix on February 15, 2021
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.
Answered by Mike Baxter on February 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP