admin管理员组文章数量:1125498
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!
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!
Share Improve this question edited Sep 26, 2013 at 16:13 leemon asked Sep 26, 2013 at 16:04 leemonleemon 2,0124 gold badges22 silver badges51 bronze badges3 Answers
Reset to default 1To 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');
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.
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' );
}
}
本文标签: sortMake custom post type column sortable
版权声明:本文标题:sort - Make custom post type column sortable 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736667458a1946738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论