admin管理员组文章数量:1125547
I created custom taxonomy named 'Customers'. In admin panel, different users will login and create their Customers in that taxonomy. I want to show them their own Customers only. Currently they can see all Customers. See this screenshot-
I created custom taxonomy named 'Customers'. In admin panel, different users will login and create their Customers in that taxonomy. I want to show them their own Customers only. Currently they can see all Customers. See this screenshot-
Share Improve this question asked Feb 1, 2024 at 0:34 Ravinder singhRavinder singh 11 bronze badge1 Answer
Reset to default 0I don't have enough time to test the full code, but you can get the idea from my code.
Taxonomies themselves don't have a direct association with authors. So there is no easy way to do it.
You can add custom meta fields to custom taxonomies also. So you can store the current user ID in a custom meta field.
function save_current_user_id_for_customers($term_id, $tt_id, $taxonomy) {
if ($taxonomy == 'customers') {
$current_user_id = get_current_user_id();
update_term_meta($term_id, 'user_id', $current_user_id);
}
}
add_action('create_term', 'save_current_user_id_for_customers', 10, 3);
Then you can use a parse_tax_query filter to filter customers.
function filter_customers_taxonomy_query($query) {
global $pagenow;
if ($pagenow == 'edit-tags.php' && isset($_GET['taxonomy']) && $_GET['taxonomy'] == 'customers') {
$current_user_id = get_current_user_id();
$tax_query = array(
array(
'taxonomy' => 'customers',
'field' => 'user_id', // Change this to the actual custom field name
'terms' => $current_user_id,
'operator' => 'IN',
),
);
$query->tax_query->queries = $tax_query;
$query->tax_query->relation = 'AND';
}
}
add_action('parse_tax_query', 'filter_customers_taxonomy_query');
本文标签: How to filter the terms of custom taxonomy by author id in admin panel
版权声明:本文标题:How to filter the terms of custom taxonomy by author id in admin panel 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736667220a1946726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论