WordPress – make a custom admin column of a custom post type sortable (the column is a Taxonomy)

huangapple go评论83阅读模式
英文:

WordPress - make a custom admin column of a custom post type sortable (the column is a Taxonomy)

问题

我创建了一个名为property的自定义文章类型(CPT),该CPT具有名为property_type的分类法。
在管理部分,我使用以下代码创建了一个自定义列,以显示property_type分类法在CPT列表中:

add_filter('manage_property_posts_columns', function($columns){
    return [
        'cb' => $columns['cb'],
        'title' => $columns['title'],
        'type' => '类型',
        'date' => $columns['date']
    ];
});

以及这个:

add_filter('manage_property_posts_custom_column', function($column, $postId){
    if($column === 'type'){
        $typePost = wp_get_post_terms($postId,['property_type']);
        echo $typePost[0]->name;
    }
}, 10, 2);

这部分代码运行正常。现在我想让这一列可排序。

在我的研究中,我找到了以下内容来使列标题可排序:

function register_sortable_columns( $columns ) {
    $columns['type'] = 'type';
    return $columns;
}
add_filter( 'manage_edit-property_sortable_columns', 'register_sortable_columns' );

以及以下内容来管理排序:

add_action( 'pre_get_posts', 'my_slice_orderby' );
function my_slice_orderby( $query ) {
	if( ! is_admin() )
		return;
	$orderby = $query->get( 'orderby');
	if( 'type' == $orderby ) {
		$query->set('meta_key','type');
		$query->set('orderby','meta_value');
	}
}

但是当我点击排序时,列表为空白。有任何想法吗?
谢谢!

英文:

i created a custom post type called property, this CPT has a taxonomy called property_type.
In the admin section I created a custom column to show the property_type taxonomy in the CTP listing with this code:

add_filter('manage_property_posts_columns', function($columns){
    return [
        'cb' => $columns['cb'],
        'title' => $columns['title'],
        'type' => 'Type',
        'date' => $columns['date']
    ];
});

and this

add_filter('manage_property_posts_custom_column', function($column, $postId){
    if($column ==='type'){
        $typePost = wp_get_post_terms($postId,['property_type']);
        echo $typePost[0]->name;
    }
}, 10, 2);

That works fine. Now I want to make this column sortable.

In my reseach I found this to make the column title sortable

function register_sortable_columns( $columns ) {
    $columns['type'] = 'type';
    return $columns;
}
add_filter( 'manage_edit-property_sortable_columns', 'register_sortable_columns' );

and this to manage to sort:

add_action( 'pre_get_posts', 'my_slice_orderby' );
function my_slice_orderby( $query ) {
	if( ! is_admin() )
		return;
	$orderby = $query->get( 'orderby');
	if( 'type' == $orderby ) {
		$query->set('meta_key','type');
		$query->set('orderby','meta_value');
	}
}

but when i click on the sort, i have a blank listing.
Any idea please?
thanks

答案1

得分: -2

以下是您提供的代码的翻译:

找到了解决方案。对于那些感兴趣的人:

function register_sortable_columns( $columns ) {
    $columns['type'] = 'type';
    return $columns;
}
add_filter( 'manage_edit- property_sortable_columns','register_sortable_columns' );


// 自定义“类型”列的排序
add_action( 'pre_get_posts', 'my_property_type_orderby' );
function my_property_type_orderby( $query ) {
    if ( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby' );
    if ( 'type' == $orderby ) {
        $query->set( 'orderby', 'taxonomy__field' );

        // 检查当前排序是升序还是降序,并反转它
        $order = strtoupper( $query->get( 'order' ) );
        if ( $order === 'ASC' ) {
            $query->set( 'order', 'DESC' );
        } else {
            $query->set( 'order', 'ASC' );
        }
    }
}

// 处理自定义排序
add_filter( 'posts_clauses', 'my_custom_tax_orderby', 10, 2 );
function my_custom_tax_orderby( $clauses, $query ) {
    global $wpdb;
    if ( isset( $query->query['orderby'] ) && 'taxonomy__field' === $query->query['orderby'] ) {
        $clauses['join'] .= "
            LEFT JOIN $wpdb->term_relationships AS tr ON ($wpdb->posts.ID = tr.object_id)
            LEFT JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
            LEFT JOIN $wpdb->terms AS t ON (tt.term_id = t.term_id)
        ";
        $clauses['where'] .= " AND (taxonomy = 'property_type' AND tt.taxonomy = 'property_type')";
        $clauses['groupby'] = "($wpdb->posts.ID)";
        $clauses['orderby'] = "t.name " . $query->get( 'order' ); // 使用查询中的顺序
    }
    return $clauses;
}

希望这有助于您理解代码。如果您有任何问题,请随时提问。

英文:

Found the solution. For those interested:

function register_sortable_columns( $columns ) {
    $columns['type'] = 'type';
    return $columns;
}
add_filter( 'manage_edit- property_sortable_columns','register_sortable_columns' );


// Custom sorting for the "type" column
add_action( 'pre_get_posts', 'my_property_type_orderby' );
function my_property_type_orderby( $query ) {
    if ( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby' );
    if ( 'type' == $orderby ) {
        $query->set( 'orderby', 'taxonomy__field' );
    
        // Check if the current order is ascending or descending and reverse it
        $order = strtoupper( $query->get( 'order' ) );
        if ( $order === 'ASC' ) {
            $query->set( 'order', 'DESC' );
        } else {
            $query->set( 'order', 'ASC' );
        }
    }
}

// Handle custom sorting
add_filter( 'posts_clauses', 'my_custom_tax_orderby', 10, 2 );
function my_custom_tax_orderby( $clauses, $query ) {
    global $wpdb;
    if ( isset( $query->query['orderby'] ) && 'taxonomy__field' === $query->query['orderby'] ) {
        $clauses['join'] .= "
            LEFT JOIN $wpdb->term_relationships AS tr ON ($wpdb->posts.ID = tr.object_id)
            LEFT JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
            LEFT JOIN $wpdb->terms AS t ON (tt.term_id = t.term_id)
    ";
        $clauses['where'] .= " AND (taxonomy = 'property_type' AND tt.taxonomy = 'property_type')";
           $clauses['groupby'] = "($wpdb->posts.ID)";
        $clauses['orderby'] = "t.name " . $query->get( 'order' ); // Use the order from the query
    }
    return $clauses;

}

huangapple
  • 本文由 发表于 2023年7月23日 15:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定